Skip to content

Commit

Permalink
Split rakefile into parts (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe authored and AliSoftware committed Mar 1, 2017
1 parent a63622b commit 8d83daf
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 76 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ _None_

### Internal Changes

_None_
* Switch from Travis CI to Circle CI, clean up the Rakefile in the process.
[David Jennes](https://github.com/djbe)
[#20](https://github.com/SwiftGen/SwiftGenKit/issues/20)
[#25](https://github.com/SwiftGen/SwiftGenKit/issues/25)

## 1.0.0

Expand Down
83 changes: 8 additions & 75 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,79 +1,12 @@
def xcpretty(cmd, task)
name = task.name.gsub(/:/,"_")
xcpretty = `which xcpretty`
#!/usr/bin/rake

if ENV['CI']
sh "set -o pipefail && #{cmd} | tee \"#{ENV['CIRCLE_ARTIFACTS']}/#{name}_raw.log\" | xcpretty --color --report junit --output \"#{ENV['CIRCLE_TEST_REPORTS']}/xcode/#{name}.xml\""
elsif xcpretty && $?.success?
sh "set -o pipefail && #{cmd} | xcpretty -c"
else
sh cmd
end
end
## [ Constants ] ##############################################################

def plain(cmd, task)
name = task.name.gsub(/:/,"_")
if ENV['CI']
sh "set -o pipefail && #{cmd} | tee \"#{ENV['CIRCLE_ARTIFACTS']}/#{name}_raw.log\""
else
sh cmd
end
end
WORKSPACE = 'StencilSwiftKit'
TARGET_NAME = 'Tests'
CONFIGURATION = 'Debug'
POD_NAME = 'StencilSwiftKit'
TEST_PATH = "Tests/#{POD_NAME}Tests"

namespace :spm do
desc 'Build using SPM'
task :build do |task|
plain("swift build", task)
end

desc 'Run SPM Unit Tests'
task :test => :build do |task|
plain("swift test", task)
end
end

namespace :xcode do
desc 'Build using Xcode'
task :build do |task|
xcpretty("xcodebuild -workspace StencilSwiftKit.xcworkspace -scheme Tests build-for-testing", task)
end

desc 'Run Xcode Unit Tests'
task :test => :build do |task|
xcpretty("xcodebuild -workspace StencilSwiftKit.xcworkspace -scheme Tests test-without-building", task)
end
end

namespace :lint do
desc 'Install swiftlint'
task :install do |task|
swiftlint = `which swiftlint`

if !(swiftlint && $?.success?)
url = 'https://github.com/realm/SwiftLint/releases/download/0.16.1/SwiftLint.pkg'
tmppath = '/tmp/SwiftLint.pkg'

plain("curl -Lo #{tmppath} #{url}", task)
plain("sudo installer -pkg #{tmppath} -target /", task)
end
end

desc 'Lint the code'
task :code => :install do |task|
plain("swiftlint lint --no-cache --strict --path Sources", task)
end

desc 'Lint the tests'
task :tests => :install do |task|
plain("swiftlint lint --no-cache --strict --path Tests/StencilSwiftKitTests", task)
end
end

namespace :pod do
desc 'Lint the Pod'
task :lint do |task|
plain("pod lib lint StencilSwiftKit.podspec --quick", task)
end
end

task :default => "xcode:test"
task :default => 'xcode:test'
15 changes: 15 additions & 0 deletions StencilSwiftKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
buildConfigurationList = 09A87B551BCCA2C600D9B9F5 /* Build configuration list for PBXNativeTarget "Tests" */;
buildPhases = (
C3989F9D7584D08720E81BDA /* [CP] Check Pods Manifest.lock */,
DD3EE1B91E65D8BC00EA1599 /* ⚠️ SwiftLint */,
09A87B4C1BCCA2C600D9B9F5 /* Sources */,
09A87B4D1BCCA2C600D9B9F5 /* Frameworks */,
09A87B4E1BCCA2C600D9B9F5 /* Resources */,
Expand Down Expand Up @@ -238,6 +239,20 @@
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
DD3EE1B91E65D8BC00EA1599 /* ⚠️ SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "⚠️ SwiftLint";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if [[ ! $CI ]]; then\n rake lint:code\n rake lint:tests\nfi";
};
DE127EA8748200EF28090721 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
26 changes: 26 additions & 0 deletions rakelib/lint.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace :lint do
desc 'Install swiftlint'
task :install do |task|
if !system('which swiftlint > /dev/null')
url = 'https://github.com/realm/SwiftLint/releases/download/0.16.1/SwiftLint.pkg'
tmppath = '/tmp/SwiftLint.pkg'

Utils.run([
"curl -Lo #{tmppath} #{url}",
"sudo installer -pkg #{tmppath} -target /"
], task)
end
end

desc 'Lint the code'
task :code => :install do |task|
Utils.print_info 'Linting the code'
Utils.run(%Q(swiftlint lint --no-cache --strict --path Sources), task)
end

desc 'Lint the tests'
task :tests => :install do |task|
Utils.print_info 'Linting the unit test code'
Utils.run(%Q(swiftlint lint --no-cache --strict --path "#{TEST_PATH}"), task)
end
end
7 changes: 7 additions & 0 deletions rakelib/pod.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace :pod do
desc 'Lint the Pod'
task :lint do |task|
Utils.print_info 'Linting the pod spec'
Utils.run(%Q(pod lib lint "#{POD_NAME}.podspec" --quick), task)
end
end
13 changes: 13 additions & 0 deletions rakelib/spm.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace :spm do
desc 'Build using SPM'
task :build do |task|
Utils.print_info 'Compile using SPM'
Utils.run("swift build", task, xcrun: true)
end

desc 'Run SPM Unit Tests'
task :test => :build do |task|
Utils.print_info 'Run the unit tests using SPM'
Utils.run("swift test", task, xcrun: true)
end
end
68 changes: 68 additions & 0 deletions rakelib/utils.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class Utils

# run a command using xcrun and xcpretty if applicable
def self.run(cmd, task, subtask = '', xcrun: false, xcpretty: false, direct: false)
commands = xcrun ? [*cmd].map { |cmd|
"#{version_select} xcrun #{cmd}"
} : [*cmd]

if xcpretty
xcpretty(commands, task, subtask)
elsif !direct
plain(commands, task, subtask)
else
`#{commands.join(' && ')}`
end
end

# print an info header
def self.print_info(str)
(red,clr) = (`tput colors`.chomp.to_i >= 8) ? %W(\e[33m \e[m) : ["", ""]
puts red, "== #{str.chomp} ==", clr
end

## [ Private helper functions ] ##################################################

# run a command, pipe output through 'xcpretty' and store the output in CI artifacts
def self.xcpretty(cmd, task, subtask)
name = (task.name + (subtask.empty? ? '' : "_#{subtask}")).gsub(/[:-]/, "_")
command = [*cmd].join(' && ')

if ENV['CI']
Rake.sh "set -o pipefail && (#{command}) | tee \"#{ENV['CIRCLE_ARTIFACTS']}/#{name}_raw.log\" | xcpretty --color --report junit --output \"#{ENV['CIRCLE_TEST_REPORTS']}/xcode/#{name}.xml\""
elsif system('which xcpretty > /dev/null')
Rake.sh "set -o pipefail && (#{command}) | xcpretty -c"
else
Rake.sh command
end
end
private_class_method :xcpretty

# run a command and store the output in CI artifacts
def self.plain(cmd, task, subtask)
name = (task.name + (subtask.empty? ? '' : "_#{subtask}")).gsub(/[:-]/, "_")
command = [*cmd].join(' && ')

if ENV['CI']
Rake.sh "set -o pipefail && (#{command}) | tee \"#{ENV['CIRCLE_ARTIFACTS']}/#{name}_raw.log\""
else
Rake.sh command
end
end
private_class_method :plain

# select the xcode version we want/support
def self.version_select
xcodes = `mdfind "kMDItemCFBundleIdentifier = 'com.apple.dt.Xcode' && kMDItemVersion = '8.*'"`.chomp.split("\n")
if xcodes.empty?
raise "\n[!!!] You need to have Xcode 8.x to compile SwiftGen.\n\n"
end

# Order by version and get the latest one
vers = lambda { |path| `mdls -name kMDItemVersion -raw "#{path}"` }
latest_xcode_version = xcodes.sort { |p1, p2| vers.call(p1) <=> vers.call(p2) }.last
%Q(DEVELOPER_DIR="#{latest_xcode_version}/Contents/Developer")
end
private_class_method :version_select

end
13 changes: 13 additions & 0 deletions rakelib/xcode.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace :xcode do
desc 'Build using Xcode'
task :build do |task|
Utils.print_info 'Compile using Xcode'
Utils.run(%Q(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{TARGET_NAME}" -configuration "#{CONFIGURATION}" build-for-testing), task, xcrun: true, xcpretty: true)
end

desc 'Run Xcode Unit Tests'
task :test => :build do |task|
Utils.print_info 'Run the unit tests using Xcode'
Utils.run(%Q(xcodebuild -workspace "#{WORKSPACE}.xcworkspace" -scheme "#{TARGET_NAME}" -configuration "#{CONFIGURATION}" test-without-building), task, xcrun: true, xcpretty: true)
end
end

0 comments on commit 8d83daf

Please sign in to comment.