Class: MrHyde::Builder

Inherits:
Object
  • Object
show all
Includes:
Quik::Wizard
Defined in:
lib/mrhyde/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Builder

Returns a new instance of Builder.



21
22
23
24
25
26
# File 'lib/mrhyde/builder.rb', line 21

def initialize( opts={} )
  puts "starting new MrHyde script (sitefile) #{opts.inspect}; lets go"
  
  @test       = opts[:dry_run] || opts[:test] || false
  @output_dir = opts[:o] || '.'
end

Class Method Details

.load(code, opts = {}) ⇒ Object



12
13
14
15
16
# File 'lib/mrhyde/builder.rb', line 12

def self.load( code, opts={} )
  builder = Builder.new( opts )
  builder.instance_eval( code )
  builder
end

.load_file(path, opts = {}) ⇒ Object



7
8
9
10
# File 'lib/mrhyde/builder.rb', line 7

def self.load_file( path, opts={} )
  code = File.read_utf8( path )
  self.load( code, opts )
end

Instance Method Details

#config(opts = {}) {|c| ... } ⇒ Object

Yields:

  • (c)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mrhyde/builder.rb', line 65

def config( opts={} )
  puts "  handle config block"
  c = Quik::OpenConfig.new
  yield( c )
  ## pp c
  h = c.to_h

  ##
  # check for site.url  if present also
  #  add site.baseurl|path

  site_url = h['url']

  if site_url.nil? || site_url.empty?        ## special case site_url is empty string ""
    ## note: always add site url for now
    ## todo/fix: warn if we overwrite "empty" site.url - why? why not?
    h['url']     = 'http://example.com'   # note: no trailing slash (/) e.g. example.com/
    h['baseurl'] = ''                     # note: no trailing slash (/) e.g. /
    h['path']    = ''                     # (better) alias for baseurl
  else
    ## site_baseurl = h['baseurl']
    ## site_path    = h['path']

    ### calculate path/baseurl
    url = URI.parse( site_url )
    path = url.path.sub(/\/$/, '' )  ## note: cut off trailing slash if present e.g. '/' becomes ''

    h['baseurl'] = path
    h['path']    = path
    ## todo/fix: warn if we overwrite baseurl/path we new values - why? why not?
  end

  pp h


  if test?
    ## do nothing; dry run
  else
    org = YAML.load_file( "#{@output_dir}/#{@theme_key}/_config.yml" )
    pp org

    ## for now always add props at the end
    ##   improve later (patch using regex etc. - why? why not?)

    new_settings = YAML.dump( h )
    ## note: cut off leading --- if present
    new_settings = new_settings.sub( /^-{3}\s*/, '')

    File.open( "#{@output_dir}/#{@theme_key}/_config.yml", "a" ) do |f|
      f.puts
      f.puts "######################################"
      f.puts "### Mr Hyde's Settings"
      f.puts
      f.puts new_settings
    end
  end

end

#install_theme(name, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mrhyde/builder.rb', line 33

def install_theme( name, opts= {} )
  puts "  handle install_theme #{name}, #{opts.inspect}"

  ## note for now assume name is key
  ##   e.g. always downcase (e.g. Starter => starter)
  @theme_key = key = name.downcase

  ## themes_dir = "#{DrJekyll.root}/test/data"
  ## catalog = Catalog.new( "#{themes_dir}/themes.yml" )
  url = "https://github.com/drjekyllthemes/themes/raw/master/themes.yml"
  puts "GET #{url}"    ##  todo - add color e.g. .bold.green

  if test?
    # do nothing; dry run
  else
    catalog = DrJekyll::Catalog.from_url( url )
    theme = catalog.find( key )
    if theme
      pak = DrJekyll::Package.new( key, theme )

      ## todo/fix:
      ##   add GET URL   w/ color e.g. bright/bold green
      pak.download
      pak.unzip( "#{@output_dir}/#{key}" )
    else
      ## todo: issue warning - why, why not??
      fail "*** theme '#{key}' not found; sorry"
    end
  end
end

#output_dirObject

ouptput (root) dir (defaults to . e.g. working folder)



30
# File 'lib/mrhyde/builder.rb', line 30

def output_dir() @output_dir; end

#test?Boolean

“global” builder options

Returns:

  • (Boolean)


29
# File 'lib/mrhyde/builder.rb', line 29

def test?()      @test;       end