20190821のGitに関する記事は2件です。

expressとreactで作るSPA(0) - 準備編

概要

サーバサイドをexpress、フロントエンドをreactで構成するSPAのWebアプリケーションを作る流れを書いていきます。
Ubuntu 18.04 LTSで必要なものを一からinstallしながら進めていきます。

最初はコードを書き始める前に必要な準備。

Git

インストール

Ubuntu 18.04 だと標準リポジトリからinstallでもバージョン2.17.1が落ちてきて、まあそれなりに新しいんですが、少し古いといえば古い。
なので以下のコマンドで最新のGitを取得します。

sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git
git --version
# -> git version 2.21.0

初期設定

以下のコマンドで最低限の初期設定をします。
これをしておかないと、Gitでcommitができません(たぶん)。

git config --global user.name "<ユーザ名>"
git config --global user.email "<メールアドレス>"

curl

デフォルトでcurl入ってないみたいなので、以下でインストール。

sudo apt install curl

node.js

node.jsのインストール方法は色々ありますが、rubyのrbenvと同じように使えて、
プロジェクトごとにnode.jsのバージョンを変えられる ndenvが良いと思います。
ndenvはもうメンテされてなくて非推奨でした。nodenvが後継みたいです。

nodenvのinstallは、直接インストールしても良いんですがanyenvというのを使用すると
他の**envもお手軽にインストールできるようになるのでそちらを使います。

詳しくはコチラ。
anyenvを使う

ここでは実行するコマンドを書いておきます。

git clone https://github.com/anyenv/anyenv ~/.anyenv
echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(anyenv init -)"' >> ./.bashrc
exec $SHELL -l
anyenv install --init
anyenv install -l # インストール可能な**envを表示
anyenv install nodenv
echo 'eval "$(nodenv init -)"' >> ~/.bashrc
exec $SHELL -l
nodenv install -l # インストール可能なバージョンの表示
nodenv install 12.3.1 # お好みのバージョンでどうぞ
nodenv rehash
nodenv global 12.3.1 # インストールしたバージョンを指定
node --version
# -> v12.3.1

Yarn

JavaScriptのパッケージマネージャには、Yarnを使います。
Yarnのインストールは以下。

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

参考

Git Download for Linux and Unix
サルでもわかるGit入門 チュートリアル1 Gitの基本 初期設定
https://github.com/anyenv/anyenv
https://github.com/nodenv/nodenv
インストール | Yarn

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

XcodeGenの導入+Carthage+CocoaPods

はじめに

多くのアプリ開発者はプロジェクトにファイルの追加、ファイルのリネーム、ファイルの移動などをよくすると思います。さらにそのプロジェクトをチームでGitを用いて開発すると思います。ただしXcodeの仕組み上、ファイルのリネームや移動等で*.xcodeprojの内容が変更されるのでGit管理でコンフリクトが頻繁に起きてしまう問題を抱えています。
コンフリクト解決に時間を取られてしまうので、チーム全体の開発スピードを鈍足にしてしまいます。
本記事では、上記の問題を解決するXcodeGenの導入方法とCarthage・CocoaPodsとの連携について説明します。

ファイル構造

説明にあたり以下のようなファイル構造となっています。

.
├── .git
├── .gitignore
├── .swiftformat
├── .swiftlint.yml
├── Cartfile
├── Cartfile.resolved
├── Carthage
├── Extensions.framework
├── Modules
├── Podfile
├── Podfile.lock
├── Pods
├── SampleApp
├── SampleAppUITests
├── SampleProject.xcodeproj
├── SampleProject.xcworkspace
└── project.yml

XcodeGenとは

https://github.com/yonaskolb/XcodeGen
XcodeGenはXcodeのプロジェクトの設定を*.yamlで定義して*.xcodeprojを生成するツールです。
なのでGitで.xcodeproj`を管理せずに.yaml`を使うことでXcode原因のコンフリクトを回避することができます。

YAMLの定義例

project.yml
name: SampleProject
options:
  bundleIdPrefix: com.messyu.sampleproject
targets:
  SampleApp:
    type: application
    platform: iOS
    deploymentTarget: "12.0"
    sources: 
      - path: SampleApp
    settings:
      configs:
        debug:
          CUSTOM_BUILD_SETTING: my_debug_value
        release:
          CUSTOM_BUILD_SETTING: my_release_value
      SWIFT_VERSION: 5.0
    dependencies:
      - target: TodoList
      - target: TodoDetail
      - carthage: RxSwift # carthageで生成したフレームワークを利用
      - framework: Extensions.framework # 自作したフレームワーク

  TodoList:
    type: framework
    platform: iOS
    deploymentTarget: 12.0
    sources:
      - path: Modules/TodoList
        buildPhase: none
    settings:
      PRODUCT_BUNDLE_IDENTIFIER: com.messyu.todolist
      INFOPLIST_FILE: Modules/TodoList/Info.plist
      SWIFT_VERSION: 5.0
    dependencies:
      - carthage: RxSwift

  TodoDetail:
    type: framework
    platform: iOS
    deploymentTarget: 12.0
    sources:
      - path: Modules/TodoDetail
        buildPhase: none
    settings:
      PRODUCT_BUNDLE_IDENTIFIER: com.messyu.tododetail
      INFOPLIST_FILE: Modules/TodoDetail/Info.plist
      SWIFT_VERSION: 5.0
    dependencies:
      - carthage: RxSwift

Carthageとの連携

XcodeGenはCarthageで生成したフレームワークを利用できるように設定することができます。

Carthageで生成したいフレームワークを記述します。

Cartfile
github "ReactiveX/RxSwift" ~> 5.0

Cartfileをもとにcarthageコマンドでフレームワークを生成します。
フレームワークはCarthage -> Build -> iOSディレクトリに生成されるので、確認することができます。

$ carthage update --platform iOS --no-use-binaries

では実際にXcodeGenコマンドで*.xcodeprojを生成しましょう。

$  xcodegen

CocoaPodsとの連携

XcodeGenで*.xcodeproj生成できたら、CocoaPodsと連携させましょう。

Podfileの定義例

Podfile
# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'

def install_swiftformat
  pod 'SwiftFormat/CLI'
  # ビルドするたびにSwiftFormatを実行させる
  script_phase :name => 'Run SwiftFormat',
               :script => 'if which "${PODS_ROOT}/SwiftFormat/CommandLineTool/swiftformat" >/dev/null; then
  "${PODS_ROOT}/SwiftFormat/CommandLineTool/swiftformat" . --self insert
else
  echo "warning: SwiftFormat not installed, download from https://github.com/nicklockwood/SwiftFormat"
fi'
end

def install_swiftlint
  pod 'SwiftLint'
 # ビルドするたびにSwiftLintを実行させる
  script_phase :name => 'Run SwiftLint',
              :script => 'if which "${PODS_ROOT}/SwiftLint/swiftlint" >/dev/null; then
  "${PODS_ROOT}/SwiftLint/swiftlint" autocorrect
  "${PODS_ROOT}/SwiftLint/swiftlint"
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi'
end

target 'SampleApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for SampleApp
  install_swiftformat
  install_swiftlint
end

target 'TodoList' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for SampleApp
  install_swiftformat
  install_swiftlint
end

target 'TodoDetail' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for SampleApp
  install_swiftformat
  install_swiftlint
end

gitignoreの設定

 *.xcodeprojが管理されないように.gitignoreに設定を記述します。
また、Carthageで作成したフレームワークとCocoaPodsのPodfile.lockは不必要なので管理対象から除外します。

.gitignore
### Carthage ###
# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
Carthage/
Cartfile.resolved

# CocoaPods - Only use to conserve bandwidth / Save time on Pushing
#           - Also handy if you have a large number of dependant pods
#           - AS PER https://guides.cocoapods.org/using/using-cocoapods.html NEVER IGNORE THE LOCK FILE
Pods/
Podfile.lock

### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Xcode Patch
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
### Xcode Patch ###
**/xcshareddata/WorkspaceSettings.xcsettings

### macOS ###
# General
.DS_Store

参考資料

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む