Class: Hera::CreateHeraConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/hera/base.rb

Class Method Summary collapse

Class Method Details

.check_configObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hera/base.rb', line 6

def self.check_config
  if (!File.exist? "_deploy_config.yml")
    CreateHeraConfig.create_deploy
  else
    CreateHeraConfig.open_deploy
  end
  
  if (!File.exist? "_hera_config.yml")
    CreateHeraConfig.create_config
  else
    CreateHeraConfig.open_config
  end
end

.create_configObject



30
31
32
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
# File 'lib/hera/base.rb', line 30

def self.create_config
  #In case a config isn't present this will generate the base _hera_config.yml.
  puts "Generating _hera_config.yml, which you can customize to your needs."
  File.open( "_hera_config.yml", "w" ) do |the_file|        
    the_file.puts "jekyll:"
    the_file.puts "  id: haml_jekyll"
    the_file.puts "  type: haml"
    the_file.puts "  ext: haml"
    the_file.puts "  src: _source/_layouts/haml/"
    the_file.puts "  prod: ../"
    the_file.puts "pages:"
    the_file.puts "  id: haml_pages"
    the_file.puts "  type: haml"
    the_file.puts "  ext: haml"
    the_file.puts "  src: _source/_layouts/pages/"
    the_file.puts "  prod: ../../"
    the_file.puts "posts:"
    the_file.puts "  id: haml_posts"
    the_file.puts "  type: haml"
    the_file.puts "  ext: haml"
    the_file.puts "  src: _source/_layouts/posts/"
    the_file.puts "  prod: ../../_posts/"
    the_file.puts "sass:"
    the_file.puts "  id: sass_style"
    the_file.puts "  type: sass"
    the_file.puts "  ext: scss"
    the_file.puts "  src: _source/_layouts/sass/"
    the_file.puts "  prod: ../../assets/"
  end
  CreateHeraConfig.open_config
end

.create_deployObject



20
21
22
23
24
25
26
27
28
# File 'lib/hera/base.rb', line 20

def self.create_deploy
  #In case a config isn't present this will generate the base _hera_config.yml.
  puts "Generating _deploy_config.yml, please add your ssh information before deploying."
  File.open( "_deploy_config.yml", "w" ) do |the_file|        
    the_file.puts "ssh_user: [email protected]"
    the_file.puts "remote_root: ~/path/to/remote/"
  end
  CreateHeraConfig.open_deploy
end

.open_configObject



73
74
75
76
77
78
79
80
81
# File 'lib/hera/base.rb', line 73

def self.open_config
  $hera_config = open('_hera_config.yml') {|f| YAML.load(f) }

  if (!$hera_config)
    CreateHeraConfig.create_config
  else 
    CreateHeraConfig.populate_config
  end
end

.open_deployObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/hera/base.rb', line 62

def self.open_deploy
  $deploy_config = open('_deploy_config.yml') {|f| YAML.load(f) }
  
  if (!$deploy_config)
    CreateHeraConfig.create_deploy
  else 
    $ssh_user = $deploy_config['ssh_user']
    $remote_root = $deploy_config['remote_root']
  end
end

.populate_configObject



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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hera/base.rb', line 83

def self.populate_config
 
  # Generate Guardfile based on _hera_config.yml
  File.open( "Guardfile", "w" ) do |the_file| 
     $hera_config.each_key { |key|
        node = $hera_config[key]
          the_file.puts "guard 'rake', :task => '" + node['id'] + "' do"
          the_file.puts "  watch(%r{" + node['src'] + "})"              
          the_file.puts "end" 
          the_file.puts ""
      }
  end
 
  # Generate Rakefile based on _hera_config.yml
  File.open( "RakeFile", "w" ) do |the_file| 
    the_file.puts "#rsync deployment parameters"
    the_file.puts "ssh_user = '" + $ssh_user + "'"
    the_file.puts "remote_root = '" + $remote_root +"'"
    the_file.puts ""
    the_file.puts "task :deploy do"
    the_file.puts "  system('rsync -avz --delete _public/ \#{ssh_user}:\#{remote_root}')"
    the_file.puts "end"
    
    $hera_config.each_key { |key|
      node = $hera_config[key]
      
        if node['type'] == 'haml'
            $ext_type = 'html'
        end
        
        if node['type'] == 'sass'
            $ext_type = 'css'            
        end   
        
        the_file.puts ""
        the_file.puts "task :" + node['id'] + " do"
        the_file.puts "  system(%{"
        the_file.puts "    cd " + node['src'] + " && for f in *." + node['ext'] + ";"
        the_file.puts "    do [ -e $f ] && " + node['type'] + " $f " + node['prod'] + "${f%." + node['ext'] +"}." + $ext_type +"; done"
        the_file.puts "  })"
        the_file.puts "end"

        #Check if source directory exists and create it if note
        if (!File.directory? Dir.pwd + "/" + node['src'])
          Dir.mkdir(Dir.pwd + "/" + node['src'])
        end

        #Check if production directory exists and create it if note
        if (!File.directory? Dir.pwd + "/" + node['src'] + node['prod'])
          Dir.mkdir(Dir.pwd + "/" + node['src'] + node['prod'])
        end
    }  
  end 
end