Class: PosthavenTheme::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/posthaven_theme/cli.rb

Constant Summary collapse

IGNORE =
%w(config.yml)
DEFAULT_WHITELIST =
%w(layouts/ assets/ config/ snippets/ templates/)
TIMEFORMAT =
"%H:%M:%S"
NON_CONFIG_COMMANDS =
%w{help configure systeminfo}

Instance Method Summary collapse

Constructor Details

#initialize(args = [], local_options = {}, config = {}) ⇒ Cli

Returns a new instance of Cli.



35
36
37
38
39
40
41
42
# File 'lib/posthaven_theme/cli.rb', line 35

def initialize(args = [], local_options = {}, config = {})
  command = config[:current_command] || config[:current_task]
  if command && !NON_CONFIG_COMMANDS.include?(command.name)
    setup_config
    validate_config!
  end
  super
end

Instance Method Details

#checkObject



45
46
47
48
49
# File 'lib/posthaven_theme/cli.rb', line 45

def check
  say('Configuration [OK]', :green)  if PosthavenTheme.asset_list
rescue PosthavenTheme::APIError => e
  report_error('Configuration [FAIL]', e)
end

#configure(api_key = nil, theme_id = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/posthaven_theme/cli.rb', line 55

def configure(api_key=nil, theme_id=nil)
  if api_key.nil?
    say('An api key is required!', :red)
    help(:configure)
  else
    config = {api_key: api_key, theme_id: theme_id}
    PosthavenTheme.config = config
    themes = PosthavenTheme.theme_list
    config[:theme_id] ||= select_theme(themes)
    create_file('config.yml', config.to_yaml)
  end
rescue PosthavenTheme::APIError
  say('Configuration Failed – Your API Key is Likely Incorrect', :red)
end

#remove(*paths) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/posthaven_theme/cli.rb', line 113

def remove(*paths)
  paths.each do |path|
    delete_asset(path, options['quiet'])
  end
  say("Done.", :green) unless options['quiet']
rescue PosthavenTheme::APIError => e
  report_error("Could not remove.", e)
end

#replace(*paths) ⇒ Object



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
# File 'lib/posthaven_theme/cli.rb', line 84

def replace(*paths)
  say('Are you sure you want to completely replace your site theme assets? ' +
      'This is not undoable.',
      :yellow)
  if ask('Continue? (Y/N): ') == 'Y'
    # only delete files on remote that are not present locally
    # files present on remote and present locally get overridden anyway
    remote_assets = if paths.empty?
                      (PosthavenTheme.asset_list.map { |a| a['path'] } - local_assets_list)
                    else
                      paths
                    end
    remote_assets.each do |asset|
      unless PosthavenTheme.ignore_files.any? { |regex| regex =~ asset }
        delete_asset(asset, options['quiet'])
      end
    end
    local_assets = paths.empty? ? local_assets_list : paths
    local_assets.each do |asset|
      send_asset(asset, options['quiet'])
    end
    say("Done.", :green) unless options['quiet']
  end
rescue PosthavenTheme::APIError => e
  report_error('Replacement failed.', e)
end

#systeminfoObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/posthaven_theme/cli.rb', line 152

def systeminfo
  ruby_version = "#{RUBY_VERSION}"
  ruby_version += "-p#{RUBY_PATCHLEVEL}" if RUBY_PATCHLEVEL
  puts "Ruby: v#{ruby_version}"
  puts "Operating System: #{RUBY_PLATFORM}"
  %w(Thor Listen HTTParty).each do |lib|
    require "#{lib.downcase}/version"
    puts "#{lib}: v" +  Kernel.const_get("#{lib}::VERSION")
  end
end

#upload(*paths) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/posthaven_theme/cli.rb', line 72

def upload(*paths)
  assets = paths.empty? ? local_assets_list : paths
  assets.each do |asset|
    send_asset(asset, options['quiet'])
  end
  say("Done.", :green) unless options['quiet']
rescue PosthavenTheme::APIError => e
  report_error('Upload Failed.', e)
end

#watchObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/posthaven_theme/cli.rb', line 127

def watch
  puts "Watching current folder: #{Dir.pwd}"
  watcher do |filename, event|
    filename = filename.gsub("#{Dir.pwd}/", '')

    next unless local_assets_list.include?(filename)
    action = if [:changed, :new].include?(event)
      :send_asset
    elsif event == :delete
      :delete_asset
    else
      raise NotImplementedError, "Unknown event -- #{event} -- #{filename}"
    end

    begin
      send(action, filename, options['quiet'])
    rescue PosthavenTheme::APIError => e
      verb = action == :send_asset ? 'save' : 'delete'
      report_error("Unable to #{verb} asset.", e)
    end
  end
end