Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework line replace functionality to support empty strings #7

Merged
merged 4 commits into from Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,17 +5,17 @@ module Actions
class GetValueFromBuildAction < Action
def self.run(params)
app_project_dir ||= params[:app_project_dir]
regex = Regexp.new(/(?<key>#{params[:key]}\s+)(?<left>[\'\"]?)(?<value>[a-zA-Z0-9\.\_]*)(?<right>[\'\"]?)(?<comment>.*)/)
value = ""
found = false
Dir.glob("#{app_project_dir}/build.gradle") do |path|
begin
File.open(path, 'r') do |file|
file.each_line do |line|
unless line.include? "#{params[:key]} " and !found
unless line.match(regex) and !found
next
end
components = line.strip.split(' ').reject { |item| item.start_with?("//") }
value = components.last.tr("\"", "").tr("\'", "")
key, left, value, right, comment = line.match(regex).captures
break
end
file.close
Expand Down
Expand Up @@ -6,19 +6,18 @@ module Actions
class SetValueInBuildAction < Action
def self.run(params)
app_project_dir ||= params[:app_project_dir]
regex = Regexp.new(/(?<key>#{params[:key]}\s+)(?<left>[\'\"]?)(?<value>[a-zA-Z0-9\.\_]*)(?<right>[\'\"]?)(?<comment>.*)/)
found = false
Dir.glob("#{app_project_dir}/build.gradle") do |path|
begin
temp_file = Tempfile.new('versioning')
File.open(path, 'r') do |file|
file.each_line do |line|
unless line.include? "#{params[:key]} " and !found
unless line.match(regex) and !found
temp_file.puts line
next
end
components = line.strip.split(' ')
value = components.last.tr("\"", "").tr("\'", "")
line.replace line.sub(value, params[:value].to_s)
line = line.gsub regex, "\\k<key>\\k<left>#{params[:value]}\\k<right>\\k<comment>"
found = true
temp_file.puts line
end
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/app/build.gradle
Expand Up @@ -4,6 +4,7 @@ android {
buildToolsVersion "24.0.2"

defaultConfig {
applicationIdSuffix ""
minSdkVersion 14
targetSdkVersion 22
versionCode 12345
Expand Down
68 changes: 68 additions & 0 deletions spec/set_value_in_build_spec.rb
Expand Up @@ -34,4 +34,72 @@ def build_tools_version
remove_fixture
end
end

describe "Set a non string value" do
before do
copy_build_fixture
end

def execute_lane_test
Fastlane::FastFile.new.parse("lane :test do
set_value_in_build(
app_project_dir: \"../**/app\",
key: \"versionCode\",
value: \"123\"
)
end").runner.execute(:test)
end

def version_code
Fastlane::FastFile.new.parse("lane :test do
get_value_from_build(
app_project_dir: \"../**/app\",
key: \"versionCode\"
)
end").runner.execute(:test)
end

it "should return incremented version code from build.gradle" do
execute_lane_test
expect(version_code).to eq("123")
end

after do
remove_fixture
end
end

describe "Set an empty string" do
before do
copy_build_fixture
end

def execute_lane_test
Fastlane::FastFile.new.parse("lane :test do
set_value_in_build(
app_project_dir: \"../**/app\",
key: \"applicationIdSuffix\",
value: \".beta\"
)
end").runner.execute(:test)
end

def application_id_suffix
Fastlane::FastFile.new.parse("lane :test do
get_value_from_build(
app_project_dir: \"../**/app\",
key: \"applicationIdSuffix\"
)
end").runner.execute(:test)
end

it "should return updated suffix from build.gradle" do
execute_lane_test
expect(application_id_suffix).to eq(".beta")
end

after do
remove_fixture
end
end
end