Skip to content

Commit 838955f

Browse files
authored
Merge pull request #15 from jdee/infer-project-location
Made :xcodeproj parameter optional (Fix #9)
2 parents b994071 + 54f7d54 commit 838955f

File tree

5 files changed

+92
-21
lines changed

5 files changed

+92
-21
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
fastlane-plugin-settings_bundle (1.1.2)
4+
fastlane-plugin-settings_bundle (1.2.0)
55
plist
66
xcodeproj (>= 1.4.0)
77

@@ -191,4 +191,4 @@ DEPENDENCIES
191191
simplecov
192192

193193
BUNDLED WITH
194-
1.14.6
194+
1.15.1

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ the current build number.
2929

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

43+
#### Specifying the project file
44+
45+
By default, the action looks for a single .xcodeproj file in the repo,
46+
excluding any under Pods. If more than one is present, use the `:xcodeproj`
47+
parameter:
48+
49+
```ruby
50+
update_settings_bundle(
51+
xcodeproj: "./MyProject.xcodeproj",
52+
key: "CurrentAppVersion",
53+
value: ":version (:build)"
54+
)
55+
```
56+
4457
#### Files other than Root.plist
4558

4659
```ruby
4760
update_settings_bundle(
48-
xcodeproj: "MyProject.xcodeproj",
4961
file: "About.plist",
5062
key: "CurrentAppVersion",
5163
value: ":version (:build)"
@@ -65,7 +77,6 @@ other settings besides the version and build numbers.
6577

6678
```ruby
6779
update_settings_bundle(
68-
xcodeproj: "MyProject.xcodeproj",
6980
key: "BuildDate",
7081
value: Time.now.strftime("%Y-%m-%d")
7182
)
@@ -81,7 +92,6 @@ different configuration, use a `configuration` parameter:
8192

8293
```ruby
8394
update_settings_bundle(
84-
xcodeproj: "MyProject.xcodeproj",
8595
key: "CurrentAppVersion",
8696
value: ":version (:build)",
8797
configuration: "Debug"
@@ -94,7 +104,6 @@ By default, this action takes the settings from the first non-test, non-extensio
94104
the project. Use the optional :target parameter to specify a target by name.
95105
```ruby
96106
update_settings_bundle(
97-
xcodeproj: "MyProject.xcodeproj",
98107
key: "CurrentAppVersion",
99108
value: ":version (:build)",
100109
target: "MyAppTarget"
@@ -107,7 +116,6 @@ By default, this action looks for a file called `Settings.bundle` in the project
107116
specify a different name for your settings bundle, use the `:bundle_name` option:
108117
```ruby
109118
update_settings_bundle(
110-
xcodeproj: "MyProject.xcodeproj",
111119
key: "CurrentAppVersion",
112120
value: ":version (:build)",
113121
bundle_name: "MySettings.bundle"

lib/fastlane/plugin/settings_bundle/actions/update_settings_bundle_action.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ def self.run(params)
2828
file = params[:file]
2929
value = params[:value]
3030

31-
# try to open project file (raises)
32-
project = Xcodeproj::Project.open params[:xcodeproj]
33-
3431
helper = Helper::SettingsBundleHelper
3532

33+
xcodeproj_path = helper.xcodeproj_path_from_params params
34+
# Error already reported in helper
35+
return if xcodeproj_path.nil?
36+
37+
# try to open project file (raises)
38+
project = Xcodeproj::Project.open xcodeproj_path
39+
3640
# raises
3741
settings = helper.settings_from_project project, configuration, target_name
3842

@@ -64,11 +68,6 @@ def self.details
6468
def self.available_options
6569
[
6670
# Required parameters
67-
FastlaneCore::ConfigItem.new(key: :xcodeproj,
68-
env_name: "SETTINGS_BUNDLE_XCODEPROJ",
69-
description: "An Xcode project file whose settings bundle to update",
70-
optional: false,
71-
type: String),
7271
FastlaneCore::ConfigItem.new(key: :key,
7372
env_name: "SETTINGS_BUNDLE_KEY",
7473
description: "The user defaults key to update in the settings bundle",
@@ -81,6 +80,11 @@ def self.available_options
8180
type: String),
8281

8382
# Optional parameters
83+
FastlaneCore::ConfigItem.new(key: :xcodeproj,
84+
env_name: "SETTINGS_BUNDLE_XCODEPROJ",
85+
description: "An Xcode project file whose settings bundle to update",
86+
optional: true,
87+
type: String),
8488
FastlaneCore::ConfigItem.new(key: :configuration,
8589
env_name: "SETTINGS_BUNDLE_CONFIGURATION",
8690
description: "The build configuration to use for the Info.plist file",
@@ -111,45 +115,46 @@ def self.example_code
111115
[
112116
<<-EOF
113117
update_settings_bundle(
114-
xcodeproj: "MyProject.xcodeproj",
115118
key: "CurrentAppVersion",
116119
value: ":version (:build)"
117120
)
118121
EOF,
119122
<<-EOF
120123
update_settings_bundle(
121124
xcodeproj: "MyProject.xcodeproj",
125+
key: "CurrentAppVersion",
126+
value: ":version (:build)"
127+
)
128+
EOF,
129+
<<-EOF
130+
update_settings_bundle(
122131
file: "About.plist",
123132
key: "CurrentAppVersion",
124133
value: ":version (:build)"
125134
)
126135
EOF,
127136
<<-EOF
128137
update_settings_bundle(
129-
xcodeproj: "MyProject.xcodeproj",
130138
key: "BuildDate",
131139
value: Time.now.strftime("%Y-%m-%d")
132140
)
133141
EOF,
134142
<<-EOF
135143
update_settings_bundle(
136-
xcodeproj: "MyProject.xcodeproj",
137144
key: "CurrentAppVersion",
138145
value: ":version (:build)",
139146
configuration: "Debug"
140147
)
141148
EOF,
142149
<<-EOF
143150
update_settings_bundle(
144-
xcodeproj: "MyProject.xcodeproj",
145151
key: "CurrentAppVersion",
146152
value: ":version (:build)",
147153
target: "MyAppTarget"
148154
)
149155
EOF,
150156
<<-EOF
151157
update_settings_bundle(
152-
xcodeproj: "MyProject.xcodeproj",
153158
key: "CurrentAppVersion",
154159
value: ":version (:build)",
155160
bundle_name: "MySettings.bundle"

lib/fastlane/plugin/settings_bundle/helper/settings_bundle_helper.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ def update_settings_plist_title_setting(project, bundle_name, file, key, value)
131131
# Save (raises)
132132
Plist::Emit.save_plist settings_plist, plist_path
133133
end
134+
135+
def xcodeproj_path_from_params(params)
136+
return params[:xcodeproj] if params[:xcodeproj]
137+
138+
# Adapted from commit_version_bump
139+
# https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/actions/commit_version_bump.rb#L21
140+
141+
# This may not be a git project. Search relative to the Gemfile.
142+
repo_path = Bundler.root
143+
144+
all_xcodeproj_paths = Dir[File.expand_path(File.join(repo_path, '**/*.xcodeproj'))]
145+
# find an xcodeproj (ignoring the Cocoapods one)
146+
xcodeproj_paths = Fastlane::Actions.ignore_cocoapods_path(all_xcodeproj_paths)
147+
148+
# no projects found: error
149+
UI.user_error!('Could not find a .xcodeproj in the current repository\'s working directory.') and return nil if xcodeproj_paths.count == 0
150+
151+
# too many projects found: error
152+
if xcodeproj_paths.count > 1
153+
repo_pathname = Pathname.new repo_path
154+
relative_projects = xcodeproj_paths.map { |e| Pathname.new(e).relative_path_from(repo_pathname).to_s }.join("\n")
155+
UI.user_error!("Found multiple .xcodeproj projects in the current repository's working directory. Please specify your app's main project: \n#{relative_projects}")
156+
return nil
157+
end
158+
159+
# one project found: great
160+
xcodeproj_paths.first
161+
end
134162
end
135163
end
136164
end

spec/settings_bundle_helper_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,34 @@
408408
end.to raise_error RuntimeError
409409
end
410410
end
411+
412+
describe 'xcodeproj_path_from_params' do
413+
let (:root) { Bundler.root }
414+
415+
it 'returns the :xcodeproj parameter if present' do
416+
expect(helper.xcodeproj_path_from_params(xcodeproj: "./MyProject.xcodeproj")).to eq "./MyProject.xcodeproj"
417+
end
418+
419+
it 'returns the path if one project present' do
420+
expect(Dir).to receive(:[]) { ["#{root}/MyProject.xcodeproj"] }
421+
expect(helper.xcodeproj_path_from_params({})).to eq "#{root}/MyProject.xcodeproj"
422+
end
423+
424+
it 'ignores projects under Pods' do
425+
expect(Dir).to receive(:[]) { ["#{root}/MyProject.xcodeproj", "#{root}/Pods/Pods.xcodeproj"] }
426+
expect(helper.xcodeproj_path_from_params({})).to eq "#{root}/MyProject.xcodeproj"
427+
end
428+
429+
it 'returns nil and errors if no project found' do
430+
expect(Dir).to receive(:[]) { [] }
431+
expect(FastlaneCore::UI).to receive(:user_error!)
432+
expect(helper.xcodeproj_path_from_params({})).to be_nil
433+
end
434+
435+
it 'returns the path if one project present' do
436+
expect(Dir).to receive(:[]) { ["#{root}/MyProject.xcodeproj", "#{root}/OtherProject.xcodeproj"] }
437+
expect(FastlaneCore::UI).to receive(:user_error!)
438+
expect(helper.xcodeproj_path_from_params({})).to be_nil
439+
end
440+
end
411441
end

0 commit comments

Comments
 (0)