Skip to content

Commit

Permalink
Handle tasks with rake (jjochen#88)
Browse files Browse the repository at this point in the history
* add Rakefile with same tasks as run.sh

* improve rakefile

* use rake in ci configuration

* fix rakefile

* fix rakefile

* fix circle config

* remove run.sh, print debug info before test, fix rakefile

* parse destination with env variable
  • Loading branch information
jjochen authored Jan 4, 2018
1 parent c2b01a2 commit 2fa4684
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 205 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ aliases:

- &run-tests
name: Build and run tests
command: ./scripts/run.sh tests "$DESTINATION"
command: bundle exec rake test_destination

- &update-codecov
name: Update Codecov
Expand Down Expand Up @@ -125,7 +125,7 @@ jobs:

- run:
name: Lint Podspec
command: bundle exec pod lib lint
command: bundle exec rake lint_podspec


build-documentation:
Expand All @@ -144,7 +144,7 @@ jobs:

- run:
name: Build Documentation
command: ./scripts/run.sh documentation
command: bundle exec rake build_documentation


swiftlint:
Expand Down Expand Up @@ -182,7 +182,7 @@ jobs:

- run:
name: Push Podspec to trunk
command: bundle exec pod trunk push
command: bundle exec rake push_podspec


workflows:
Expand Down
4 changes: 2 additions & 2 deletions Example/JJFloatingActionButton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
195269421FD7E6200053C785 /* UIColorExtensionsSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorExtensionsSpec.swift; sourceTree = "<group>"; };
195269441FD7E63B0053C785 /* JJActionItemSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JJActionItemSpec.swift; sourceTree = "<group>"; };
195637221FDA9EFB0003B8FF /* config.yml */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = text; lineEnding = 0; name = config.yml; path = ../../.circleci/config.yml; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
19746F721FEABAB200C42037 /* run.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = run.sh; path = ../../scripts/run.sh; sourceTree = "<group>"; };
198B65FD1FBAE904009A17F2 /* JJFloatingActionButton_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JJFloatingActionButton_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
198B66001FBAE904009A17F2 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
198B66021FBAE904009A17F2 /* FirstViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstViewController.swift; sourceTree = "<group>"; };
Expand All @@ -66,6 +65,7 @@
199592AB1FD73F070058BEFC /* JJFloatingActionButton.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = JJFloatingActionButton.xib; sourceTree = "<group>"; };
19B10AC21FF7BDDD00D70C32 /* .jazzy.yaml */ = {isa = PBXFileReference; lastKnownFileType = text; name = .jazzy.yaml; path = ../../.jazzy.yaml; sourceTree = "<group>"; };
19B10AD71FFCEF0800D70C32 /* JJFloationgActionButtonDelegateMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JJFloationgActionButtonDelegateMock.swift; path = ReferenceImages/JJFloationgActionButtonDelegateMock.swift; sourceTree = "<group>"; };
19B10ADB1FFE692100D70C32 /* Rakefile */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; fileEncoding = 4; indentWidth = 2; name = Rakefile; path = ../../Rakefile; sourceTree = "<group>"; tabWidth = 2; };
19DF53B81FF59AE5006E292F /* Package.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Package.swift; path = ../../Package.swift; sourceTree = "<group>"; };
19E247E41FD7E6B5009A7BA1 /* JJActionItem.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = JJActionItem.xib; sourceTree = "<group>"; };
19E247E91FD854B1009A7BA1 /* .github_changelog_generator */ = {isa = PBXFileReference; lastKnownFileType = text; name = .github_changelog_generator; path = ../../.github_changelog_generator; sourceTree = "<group>"; };
Expand Down Expand Up @@ -163,7 +163,7 @@
198B66231FBAEDD6009A17F2 /* LICENSE */,
19DF53B81FF59AE5006E292F /* Package.swift */,
198B66241FBAEDD6009A17F2 /* README.md */,
19746F721FEABAB200C42037 /* run.sh */,
19B10ADB1FFE692100D70C32 /* Rakefile */,
);
name = Metadata;
path = JJFloatingActionButton;
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ source 'https://rubygems.org' do
gem 'synx'
gem 'github_changelog_generator'
gem 'jazzy', '>= 0.9.0'
gem 'rake'
gem 'fileutils'
end

3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GEM
faraday-http-cache (2.0.0)
faraday (~> 0.8)
ffi (1.9.18)
fileutils (1.0.2)
fourflusher (2.0.1)
fuzzy_match (2.0.4)
gh_inspector (1.0.3)
Expand Down Expand Up @@ -131,8 +132,10 @@ DEPENDENCIES
cocoapods!
cocoapods-clean!
cocoapods-deintegrate!
fileutils!
github_changelog_generator!
jazzy (>= 0.9.0)!
rake!
synx!
xcpretty!

Expand Down
310 changes: 310 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
#
# Copyright (c) 2017-Present Jochen Pfeiffer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

#-- Bootstrap ----------------------------------------------------------------#

desc 'Initialize your working copy'
task :bootstrap do
unless system('which bundle')
error_message "Please install the bundler gem manually:\n" \
' $ [sudo] gem install bundler'
exit 1
end

install_gems
install_cocoapods
end


begin
require 'fileutils'

task default: :test


#-- Tests ------------------------------------------------------------------#

desc 'Run tests'
task :test do
xcodebuild_test "platform=iOS Simulator,name=iPhone X"
end

desc 'Run tests for DESTINATION env'
task :test_destination do
Rake::Task[:print_debug_info].invoke
xcodebuild_test ENV['DESTINATION']
end

desc 'Print debug info'
task :print_debug_info do
title 'Debug info'
sh 'xcodebuild -version'
sh 'xcodebuild -showsdks'
sh 'instruments -s devices'
end

desc 'Lint swift'
task :lint_swift do
unless system('which swiftlint')
error_message "Please install swiftlint manually:\n" \
' $ brew install swiftlint'
exit 1
end

title 'Linting swift'
sh "swiftlint"
end

desc 'Lint podspec'
task :lint_podspec do
title 'Linting podspec'
sh "bundle exec pod lib lint"
end


#-- Format -----------------------------------------------------------------#

desc 'Format code'
task :format do
unless system('which swiftformat')
error_message "Please install swiftformat manually:\n" \
' $ brew install swiftformat'
exit 1
end

title 'Formating code'
sh "swiftformat Example/Tests Example/JJFloatingActionButton Sources"
end

desc 'Format and lint code'
task :format_and_lint do
Rake::Task[:format].invoke
Rake::Task[:lint_swift].invoke
end


#-- Dependencies -----------------------------------------------------------#

desc 'Install dependencies'
task :install_dependencies do
install_gems
install_cocoapods
end

desc 'Update dependencies'
task :update_dependencies do
update_gems
update_cocoapods
end


#-- Documentation ----------------------------------------------------------#

desc 'Build documentation'
task :build_documentation do
generate_documentation
end


#-- Changelog --------------------------------------------------------------#

desc 'Generate changelog'
task :generate_changelog do
generate_changelog ""
end


#-- Record Video -----------------------------------------------------------#

desc 'Record video booted simulator and convert to gif'
task :record_gif do
unless system('which ffmpeg')
error_message "Please install ffmpeg manually:\n" \
' $ brew install ffmpeg'
exit 1
end

title 'Recording video'

mov_path='./Images/JJFloatingActionButton.mov'

trap('SIGINT') { puts }
%x{xcrun simctl io booted recordVideo #{mov_path}}

title 'Convert to gif'

gif_path='./Images/JJFloatingActionButton.gif'
palette_path='./Images/palette.png'
filters='fps=30,setpts=1*PTS,scale=250:-1:flags=lanczos'

sh "ffmpeg -v warning -i #{mov_path} -vf '#{filters},palettegen' -y #{palette_path}"
if File.exist? palette_path
sh "ffmpeg -v warning -i #{mov_path} -i #{palette_path} -lavfi '#{filters} [x]; [x][1:v] paletteuse' -y #{gif_path}"
File.delete palette_path
end
end


#-- Release ----------------------------------------------------------------#

desc 'Release version'
task :release_version, :version do |task, args|
ensure_clean_git_status
update_version_in_podspec args.version
update_version_in_example_project args.version
generate_changelog args.version
install_cocoapods
generate_documentation
create_release_branch_and_commit args.version
open_pull_request args.version
end

desc 'Push podspec'
task :push_podspec do
title "Pushing podspec"
sh 'bundle exec pod trunk push'
end

rescue LoadError, NameError => e
error_message 'Some Rake tasks haven been disabled because the environment' \
' couldn’t be loaded. Be sure to run `rake bootstrap` first.'
$stderr.puts e.message
$stderr.puts e.backtrace
$stderr.puts
end


#-- Helpers ------------------------------------------------------------------#

private

def title(title)
cyan_title = "\033[0;36m#{title}\033[0m"
puts
puts '-' * 80
puts cyan_title
puts '-' * 80
puts
end

def error_message(message)
red_message = "\033[0;31m[!] #{message}\e[0m"
$stderr.puts
$stderr.puts red_message
$stderr.puts
end

def check_parameter(parameter)
if parameter.nil? || parameter.empty?
error_message "parameter can't be empty."
exit 1
end
end

def install_gems
title 'Installing gems'
sh 'bundle install'
end

def install_cocoapods
title 'Installing cocoapods'
sh 'bundle exec pod install --project-directory=Example'
end

def update_gems
title 'Updating gems'
sh 'bundle update'
end

def update_cocoapods
title 'Updating cocoapods'
sh 'bundle exec pod update --project-directory=Example'
end

def xcodebuild_test(destination)
title 'Running tests'
check_parameter(destination)
sh "xcodebuild clean build test" \
" -workspace Example/JJFloatingActionButton.xcworkspace" \
" -scheme JJFloatingActionButton_Example" \
" -sdk iphonesimulator" \
" -destination \"#{destination}\"" \
" -enableCodeCoverage YES" \
" CODE_SIGNING_REQUIRED=NO" \
" CODE_SIGN_IDENTITY=" \
" PROVISIONING_PROFILE="
end

def ensure_clean_git_status
title "Ensuring clean git status"
unless `git diff --shortstat 2> /dev/null | tail -n1` == ''
error_message "Uncommited changes. Commit first."
exit 1
end
end

def update_version_in_example_project(version)
title "Updating version in example project"
check_parameter(version)
sh "/usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString #{version}\" ./Example/JJFloatingActionButton/Info.plist"
end

def generate_changelog(version)
title "Generating changelog"
unless version.nil? || version.empty?
sh "github_changelog_generator --future-release #{version}"
else
sh "github_changelog_generator"
end
end

def generate_documentation
title 'Generating documentation'
sh 'bundle exec jazzy'
end

def update_version_in_podspec(version)
title "Updating version in podspec"
check_parameter(version)
file_name = 'JJFloatingActionButton.podspec'
contents = File.read(file_name)
new_contents = contents.gsub(/(spec\.version\s*=\s*)'.*'/, "\\1'#{version}'")
puts new_contents
File.open(file_name, "w") {|file| file.puts new_contents }
end

def create_release_branch_and_commit(version)
title "Creating release branch and commit"
check_parameter(version)
release_branch = "release/#{version}"
sh "git checkout -b #{release_branch}"
sh "git add --all"
sh "git commit -v -m 'Release #{version}'"
sh "git push --set-upstream origin #{release_branch}"
end

def open_pull_request(version)
title "Opening pull request"
check_parameter(version)
sh "open 'https://github.com/jjochen/JJFloatingActionButton/compare/release%2F#{version}?expand=1'"
end

Loading

0 comments on commit 2fa4684

Please sign in to comment.