Skip to content

Commit

Permalink
introduce rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Jul 8, 2017
1 parent 43349e8 commit cc0685e
Show file tree
Hide file tree
Showing 23 changed files with 308 additions and 232 deletions.
50 changes: 50 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
AllCops:
TargetRubyVersion: 2.4
Include:
- '**/Gemfile'
- '**/Rakefile'
- '**/rakefile'
Exclude:
- '_node/**/*'

Metrics/LineLength:
Max: 126

Layout/SpaceAroundEqualsInParameterDefault:
EnforcedStyle: no_space

Metrics/MethodLength:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/BlockLength:
Enabled: false

Metrics/BlockNesting:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/ClassLength:
Enabled: false

Style/Documentation:
Enabled: false

Layout/IndentArray:
Enabled: false

Style/GuardClause:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Style/TernaryParentheses:
Enabled: false

Style/RegexpLiteral:
Enabled: false
14 changes: 8 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# frozen_string_literal: true

source 'http://rubygems.org'

gem 'jekyll', :git => 'https://github.com/binaryage/jekyll.git', :branch => 'binaryage-site'
gem 'rdiscount'
gem 'closure-compiler'
gem 'coffee-script'
gem 'colored2'
gem 'html_press', git: 'https://github.com/binaryage/html_press'
gem 'jekyll', git: 'https://github.com/binaryage/jekyll.git', branch: 'binaryage-site'
gem 'rdiscount'
gem 'stylus'
gem 'tilt'
gem 'coffee-script'
gem 'closure-compiler'
gem 'html_press', :git => 'https://github.com/binaryage/html_press'
gem 'yui-compressor'

group :jekyll_plugins do
gem 'jekyll-contentblocks'
gem 'jekyll-coffeescript'
gem 'jekyll-contentblocks'
gem 'jekyll-redirect-from'
end
65 changes: 37 additions & 28 deletions _lib/build.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'digest/sha1'
require 'pathname'
require 'yaml'
Expand All @@ -9,7 +11,7 @@ def prepare_jekyll_config(site, opts)
dev_mode = opts[:dev_mode]
stage = opts[:stage]
busters = opts[:busters]
domain = 'binaryage.'+(dev_mode ? 'org' : 'com')
domain = 'binaryage.' + (dev_mode ? 'org' : 'com')

begin
config = YAML.load_file('_config.yml')
Expand All @@ -27,49 +29,47 @@ def prepare_jekyll_config(site, opts)
config['markdown'] = 'rdiscount'
config['dev'] = dev_mode
config['stylus'] = {
'compress' => (not dev_mode),
'debug' => dev_mode,
'path' => './shared/css/site.styl'
'compress' => !dev_mode,
'debug' => dev_mode,
'path' => './shared/css/site.styl'
}
config['combinejs'] = [{
'path' => './shared/js/code.list',
'minify' => (not dev_mode)
},{
'path' => './shared/js/changelog.list',
'minify' => (not dev_mode)
'path' => './shared/js/code.list',
'minify' => !dev_mode
}, {
'path' => './shared/js/changelog.list',
'minify' => !dev_mode
}]
config['html_press'] = {
'compress' => (not dev_mode),
'cache' => File.join(stage, '_cache')
'compress' => !dev_mode,
'cache' => File.join(stage, '_cache')
}
config['static_cdn'] = {
'enabled' => (not dev_mode and opts[:cdn]),
'zone' => File.join(stage, '_cdn'),
'url' => opts[:static_cdn_url],
'push_url' => opts[:static_cdn_push_url]
'enabled' => (!dev_mode && opts[:cdn]),
'zone' => File.join(stage, '_cdn'),
'url' => opts[:static_cdn_url],
'push_url' => opts[:static_cdn_push_url]
}
config['busterizer'] = {
'css' => (busters and (not dev_mode)),
'html' => (busters and (not dev_mode))
'css' => (busters && !dev_mode),
'html' => (busters && !dev_mode)
}
config['purge_cdn'] = true
config['sass'] ||= {}
config['sass']['load_paths'] = ['shared/css']

if opts[:dont_prune]
config.delete('prune_files')
end
config.delete('prune_files') if opts[:dont_prune]

output = YAML.dump(config)
sha = Digest::SHA1.hexdigest output
sha = sha[0..7]

configs_dir = File.join(stage, '.configs')
FileUtils.mkdir_p(configs_dir)
config_path = File.join(configs_dir, site.name+'_jekyll_config_'+sha+'.yml')
config_path = File.join(configs_dir, site.name + '_jekyll_config_' + sha + '.yml')
File.open(config_path, 'w') { |f| f.write(output) }

Pathname.new(config_path).relative_path_from(Pathname.new Dir.pwd).to_s
Pathname.new(config_path).relative_path_from(Pathname.new(Dir.pwd)).to_s
end

def build_site(site, opts)
Expand All @@ -84,7 +84,7 @@ def build_site(site, opts)
end

# noinspection RubyResolve
puts '=> ' + "#{dest}".magenta
puts '=> ' + dest.to_s.magenta
end

def build_sites(sites, opts, names)
Expand All @@ -101,13 +101,21 @@ def build_sites(sites, opts, names)
def serve_site(site, base_dir)
port = site.port
Dir.chdir site.dir do
config_path = prepare_jekyll_config(site, {:dev_mode => true,
:stage => base_dir})
opts = { dev_mode: true,
stage: base_dir }
config_path = prepare_jekyll_config(site, opts)
work_dir = File.join(base_dir, site.name)
FileUtils.mkdir_p(work_dir)
fork do
trap('INT') { exit 11 }
sys("bundle exec jekyll serve --incremental --drafts --port 1#{port} -b / --config \"#{config_path}\" --destination \"#{work_dir}\"")
cmd = 'bundle exec jekyll serve '\
'--incremental '\
'--drafts '\
"--port 1#{port} "\
'-b / '\
"--config \"#{config_path}\" "\
"--destination \"#{work_dir}\""
sys(cmd)
end
sleep(0.2)
fork do
Expand All @@ -116,11 +124,12 @@ def serve_site(site, base_dir)
node_dir = File.join(Dir.pwd, '..', '_node')
# we have to switch to _node dir with package.json and node_modules, node/browser-sync expectes it for loading plugins
Dir.chdir node_dir do
verbosity = '--logLevel info' #'--logLevel debug'
verbosity = '--logLevel info' # '--logLevel debug'
submisivity = '--no-ui --no-online --no-open'
plugins = " --plugins \"bs-html-injector?files[]=#{work_dir}/**/*.html&prefix=#{work_dir}\""
locations = "--port #{port} --proxy http://localhost:1#{port} --files \"#{work_dir}/**/*.css\""
sys("node_modules/.bin/browser-sync start #{verbosity} #{submisivity} #{plugins} #{locations}")
cmd = "node_modules/.bin/browser-sync start #{verbosity} #{submisivity} #{plugins} #{locations}"
sys(cmd)
end
end
sleep(0.2)
Expand Down
8 changes: 5 additions & 3 deletions _lib/repo.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# frozen_string_literal: true

require_relative 'utils.rb'

# prevents git error message:
# You can't push to git://github.com/darwin/site.git
# Use [email protected]:darwin/site.git
def get_writable_git_url
def writable_git_url
`git remote show origin | grep "Fetch URL:"`.strip =~ /Fetch URL:\s*(.*)/

if $1.nil?
if Regexp.last_match(1).nil?
puts "unable to parse: #{res} (ouput from: git remote show origin | grep \"Fetch URL:\")"
exit 2
end

$1.sub('git://', 'git@').sub('github.com/', 'github.com:')
Regexp.last_match(1).sub('git://', 'git@').sub('github.com/', 'github.com:')
end
8 changes: 5 additions & 3 deletions _lib/site.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'utils.rb'

class Site
Expand All @@ -8,14 +10,15 @@ def initialize(dir, port, domain)
@port = port
@name = extract_name(dir)
@subdomain = extract_subdomain(dir)
@domain = @subdomain+'.'+domain
@domain = @subdomain + '.' + domain
end

def to_s
self.inspect
inspect
end

private

# "some/path/to/repo" => "repo"
def extract_name(path)
path.split('/').last # get last component of the path
Expand All @@ -25,5 +28,4 @@ def extract_name(path)
def extract_subdomain(path)
extract_name(path).split('-')[0] # strips -web postfixes
end

end
24 changes: 16 additions & 8 deletions _lib/store.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'utils.rb'
require_relative 'build.rb'

Expand Down Expand Up @@ -51,7 +53,7 @@ def build_store(site, opts)
build_site(site, opts) # no cache busters, no cdn

build_path = File.join(stage, site.name)
template_path = File.join(build_path, (ENV['STORE_TEMPLATE'] or 'store-template.html'))
template_path = File.join(build_path, (ENV['STORE_TEMPLATE'] || 'store-template.html'))
working_dir = File.join(stage, '_storetemplate')
window_template = "#{working_dir}/window.xhtml"

Expand All @@ -67,21 +69,27 @@ def build_store(site, opts)
patch(window_template, [
['<!DOCTYPE html>', ''],
['<html ', "<html xmlns=\"http://www.w3.org/1999/xhtml\"\n "],
[/<body(.*?)>/, "<body\\1><div id=\"page-store-template\">"],
[/<body(.*?)>/, '<body\\1><div id="page-store-template">'],
['</body>', '</div></body>'],
[/<script(.*?)>/, "<script\\1>//<![CDATA[\n"],
[/<\/script>/, "\n//]]></script>"],
[/href="\/([^\/])/, "href=\"\\1"],
[/src="\/([^\/])/, "src=\"\\1"],
[/href="\/([^\/])/, 'href="\\1'],
[/src="\/([^\/])/, 'src="\\1'],
['&nbsp;', '&#160;'],
['&copy;', '&#169;'],
['##INSERT STORE CONTENT HERE##', "\n<!-- TemplateBeginEditable name=\"Content\" -->\n\n<!-- TemplateEndEditable -->"],
['</title>', "</title>\n<link title=\"main\" rel=\"stylesheet\" href=\"http://resource.fastspring.com/app/s/style/base.css\" media=\"screen,projection\" type=\"text/css\" />\n<link title=\"main\" rel=\"stylesheet\" href=\"http://resource.fastspring.com/app/store/style/base.css\" media=\"screen,projection\" type=\"text/css\" />"]
['</title>', "</title>\n"\
'<link title="main" rel="stylesheet" '\
'href="http://resource.fastspring.com/app/s/style/base.css" '\
"media=\"screen,projection\" type=\"text/css\" />\n"\
'<link title="main" rel="stylesheet" '\
'href="http://resource.fastspring.com/app/store/style/base.css" '\
'media="screen,projection" type="text/css" />']
])

content = File.read(window_template)
content.gsub!(/<!-- SCRIPTS START -->(.*?)<!-- SCRIPTS END -->(.*?)<body(.*?)>/m, "\\2<body\\3>\\1")
content.gsub!(/src="http:/m, "src=\"https:")
content.gsub!(/<!-- SCRIPTS START -->(.*?)<!-- SCRIPTS END -->(.*?)<body(.*?)>/m, '\\2<body\\3>\\1')
content.gsub!(/src="http:/m, 'src="https:')

File.open(window_template, 'w') do |f|
f << content
Expand All @@ -97,7 +105,7 @@ def build_store(site, opts)

# zip it!
zip_path = opts[:zip_path]
sys("rm \"#{zip_path}\"") if File.exists? zip_path
sys("rm \"#{zip_path}\"") if File.exist? zip_path
Dir.chdir(working_dir) do
sys("zip -r -du \"#{zip_path}\" .")
sys("du -sh \"#{zip_path}\"")
Expand Down
Loading

0 comments on commit cc0685e

Please sign in to comment.