Skip to content

Commit

Permalink
Merge pull request #15 from jdee/infer-project-location
Browse files Browse the repository at this point in the history
Made :xcodeproj parameter optional (Fix #9)
  • Loading branch information
jdee authored Jun 15, 2017
2 parents b994071 + 54f7d54 commit 838955f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fastlane-plugin-settings_bundle (1.1.2)
fastlane-plugin-settings_bundle (1.2.0)
plist
xcodeproj (>= 1.4.0)

Expand Down Expand Up @@ -191,4 +191,4 @@ DEPENDENCIES
simplecov

BUNDLED WITH
1.14.6
1.15.1
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ the current build number.

```ruby
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)"
)
Expand All @@ -41,11 +40,24 @@ specified format. Use the action this way after `increment_build_number` or
`increment_version_number` to update your settings bundle as part of a
version bump.

#### Specifying the project file

By default, the action looks for a single .xcodeproj file in the repo,
excluding any under Pods. If more than one is present, use the `:xcodeproj`
parameter:

```ruby
update_settings_bundle(
xcodeproj: "./MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)"
)
```

#### Files other than Root.plist

```ruby
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
file: "About.plist",
key: "CurrentAppVersion",
value: ":version (:build)"
Expand All @@ -65,7 +77,6 @@ other settings besides the version and build numbers.

```ruby
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "BuildDate",
value: Time.now.strftime("%Y-%m-%d")
)
Expand All @@ -81,7 +92,6 @@ different configuration, use a `configuration` parameter:

```ruby
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)",
configuration: "Debug"
Expand All @@ -94,7 +104,6 @@ By default, this action takes the settings from the first non-test, non-extensio
the project. Use the optional :target parameter to specify a target by name.
```ruby
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)",
target: "MyAppTarget"
Expand All @@ -107,7 +116,6 @@ By default, this action looks for a file called `Settings.bundle` in the project
specify a different name for your settings bundle, use the `:bundle_name` option:
```ruby
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)",
bundle_name: "MySettings.bundle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ def self.run(params)
file = params[:file]
value = params[:value]

# try to open project file (raises)
project = Xcodeproj::Project.open params[:xcodeproj]

helper = Helper::SettingsBundleHelper

xcodeproj_path = helper.xcodeproj_path_from_params params
# Error already reported in helper
return if xcodeproj_path.nil?

# try to open project file (raises)
project = Xcodeproj::Project.open xcodeproj_path

# raises
settings = helper.settings_from_project project, configuration, target_name

Expand Down Expand Up @@ -64,11 +68,6 @@ def self.details
def self.available_options
[
# Required parameters
FastlaneCore::ConfigItem.new(key: :xcodeproj,
env_name: "SETTINGS_BUNDLE_XCODEPROJ",
description: "An Xcode project file whose settings bundle to update",
optional: false,
type: String),
FastlaneCore::ConfigItem.new(key: :key,
env_name: "SETTINGS_BUNDLE_KEY",
description: "The user defaults key to update in the settings bundle",
Expand All @@ -81,6 +80,11 @@ def self.available_options
type: String),

# Optional parameters
FastlaneCore::ConfigItem.new(key: :xcodeproj,
env_name: "SETTINGS_BUNDLE_XCODEPROJ",
description: "An Xcode project file whose settings bundle to update",
optional: true,
type: String),
FastlaneCore::ConfigItem.new(key: :configuration,
env_name: "SETTINGS_BUNDLE_CONFIGURATION",
description: "The build configuration to use for the Info.plist file",
Expand Down Expand Up @@ -111,45 +115,46 @@ def self.example_code
[
<<-EOF
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)"
)
EOF,
<<-EOF
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)"
)
EOF,
<<-EOF
update_settings_bundle(
file: "About.plist",
key: "CurrentAppVersion",
value: ":version (:build)"
)
EOF,
<<-EOF
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "BuildDate",
value: Time.now.strftime("%Y-%m-%d")
)
EOF,
<<-EOF
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)",
configuration: "Debug"
)
EOF,
<<-EOF
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)",
target: "MyAppTarget"
)
EOF,
<<-EOF
update_settings_bundle(
xcodeproj: "MyProject.xcodeproj",
key: "CurrentAppVersion",
value: ":version (:build)",
bundle_name: "MySettings.bundle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ def update_settings_plist_title_setting(project, bundle_name, file, key, value)
# Save (raises)
Plist::Emit.save_plist settings_plist, plist_path
end

def xcodeproj_path_from_params(params)
return params[:xcodeproj] if params[:xcodeproj]

# Adapted from commit_version_bump
# https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/actions/commit_version_bump.rb#L21

# This may not be a git project. Search relative to the Gemfile.
repo_path = Bundler.root

all_xcodeproj_paths = Dir[File.expand_path(File.join(repo_path, '**/*.xcodeproj'))]
# find an xcodeproj (ignoring the Cocoapods one)
xcodeproj_paths = Fastlane::Actions.ignore_cocoapods_path(all_xcodeproj_paths)

# no projects found: error
UI.user_error!('Could not find a .xcodeproj in the current repository\'s working directory.') and return nil if xcodeproj_paths.count == 0

# too many projects found: error
if xcodeproj_paths.count > 1
repo_pathname = Pathname.new repo_path
relative_projects = xcodeproj_paths.map { |e| Pathname.new(e).relative_path_from(repo_pathname).to_s }.join("\n")
UI.user_error!("Found multiple .xcodeproj projects in the current repository's working directory. Please specify your app's main project: \n#{relative_projects}")
return nil
end

# one project found: great
xcodeproj_paths.first
end
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/settings_bundle_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,34 @@
end.to raise_error RuntimeError
end
end

describe 'xcodeproj_path_from_params' do
let (:root) { Bundler.root }

it 'returns the :xcodeproj parameter if present' do
expect(helper.xcodeproj_path_from_params(xcodeproj: "./MyProject.xcodeproj")).to eq "./MyProject.xcodeproj"
end

it 'returns the path if one project present' do
expect(Dir).to receive(:[]) { ["#{root}/MyProject.xcodeproj"] }
expect(helper.xcodeproj_path_from_params({})).to eq "#{root}/MyProject.xcodeproj"
end

it 'ignores projects under Pods' do
expect(Dir).to receive(:[]) { ["#{root}/MyProject.xcodeproj", "#{root}/Pods/Pods.xcodeproj"] }
expect(helper.xcodeproj_path_from_params({})).to eq "#{root}/MyProject.xcodeproj"
end

it 'returns nil and errors if no project found' do
expect(Dir).to receive(:[]) { [] }
expect(FastlaneCore::UI).to receive(:user_error!)
expect(helper.xcodeproj_path_from_params({})).to be_nil
end

it 'returns the path if one project present' do
expect(Dir).to receive(:[]) { ["#{root}/MyProject.xcodeproj", "#{root}/OtherProject.xcodeproj"] }
expect(FastlaneCore::UI).to receive(:user_error!)
expect(helper.xcodeproj_path_from_params({})).to be_nil
end
end
end

0 comments on commit 838955f

Please sign in to comment.