Skip to content

Commit

Permalink
Remove unused Runner from guard.rb and update manifest.json when file…
Browse files Browse the repository at this point in the history
…s are added/removed
  • Loading branch information
Mikk Pristavka committed Jan 9, 2014
1 parent d9bdc0e commit f058b3c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
13 changes: 7 additions & 6 deletions bin/edicy
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ desc 'Fetches the layout and layout asset files for the given site'
arg_name 'Describe arguments to pull here'
command :pull do |c|
c.action do |global_options,options,args|
@filemanager ||= Edicy::Dtk::FileManager.new
@filemanager.create_folders
@filemanager.create_files
$filemanager ||= Edicy::Dtk::FileManager.new
$filemanager.create_folders
$filemanager.create_files
end
end

Expand All @@ -71,8 +71,8 @@ desc "Generates a manifest.json file from the site's layout and asset files"
arg_name 'Describe arguments to manifest here'
command :manifest do |c|
c.action do |global_options, options, args|
@filemanager ||= Edicy::Dtk::FileManager.new
@filemanager.generate_manifest
$filemanager ||= Edicy::Dtk::FileManager.new
$filemanager.generate_manifest
end
end

Expand All @@ -81,8 +81,9 @@ arg_name 'Describe arguments to watch here'
command :watch do |c|
c.action do |global_options, options, args|
$renderer ||= Edicy::Dtk::Renderer.new(Dir.pwd)
$filemanager ||= Edicy::Dtk::FileManager.new

Edicy::Dtk::Guuard.new($renderer).run
Edicy::Dtk::Guuard.new($renderer, $filemanager).run

while ::Guard.running do
sleep 0.5
Expand Down
35 changes: 35 additions & 0 deletions lib/edicy/dtk/filemanager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ def initialize

end

def add_to_manifest(files)
@manifest = JSON.parse(File.read('manifest.json')).to_h
files.each do |file|
match = /^(component|layout)s\/(.*)/.match(file)
type, filename = match[1], match[2]
component = type == "component"
layout = {
:content_type => component ? "component" : "page",
:component => component,
:file => file,
:layout_name => component ? "" : filename.split(".").first,
:title => filename.split(".").first.gsub("_", " ").capitalize
}
@manifest["layouts"] << layout
puts "Added #{file} to manifest.json"
end
File.open('manifest.json', 'w+') do |file|
file << @manifest.to_json
end
end

def remove_from_manifest(files)
@manifest = JSON.parse(File.read('manifest.json')).to_h
files.each do |file|
@manifest["layouts"].delete_if { |layout|
match = layout["file"] == file
puts "Removed #{file} from manifest.json" if match
match
}
end
File.open('manifest.json', 'w+') do |file|
file << @manifest.to_json
end
end

def generate_manifest
layouts = Edicy.layouts
layout_assets = Edicy.layout_assets
Expand Down
51 changes: 18 additions & 33 deletions lib/edicy/dtk/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,9 @@ def run_on_modifications(res)

module Edicy::Dtk

class Runner
def initialize(options = {})

end

def run_all
puts 'Runner run all'
end

def run(paths)
puts 'Runner run'
puts paths.inspect
end
end

class ::Guard::Yoyo < ::Guard::Plugin

attr_accessor :options, :runner, :renderer
attr_accessor :options, :renderer, :filemanager

# Initializes a Guard plugin.
# Don't do any work here, especially as Guard plugins get initialized even if they are not in an active group!
Expand All @@ -57,7 +42,6 @@ def initialize(options = {})
super

@options = options
@runner = Runner.new(@options)
end

# Called once when Guard starts. Please override initialize method to init stuff.
Expand Down Expand Up @@ -98,7 +82,6 @@ def stop
#
def run_all
::Guard::UI.info 'Guard::Edicy re-render all'
# runner.run_all
renderer.render_pages
end

Expand All @@ -108,7 +91,6 @@ def run_all
# @return [Object] the task result
#
# def run_on_changes(paths)
# puts paths
# puts 'YOYO run on changes'
# end

Expand All @@ -118,9 +100,19 @@ def run_all
# @raise [:task_has_failed] when run_on_additions has failed
# @return [Object] the task result
#
# def run_on_additions(paths)
# puts 'YOYO run on additions'
# end
def run_on_additions(paths)
@filemanager.add_to_manifest paths
end

# Called on file(s) removals that the Guard plugin watches.
#
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_removals has failed
# @return [Object] the task result
#
def run_on_removals(paths)
@filemanager.remove_from_manifest paths
end

# Called on file(s) modifications that the Guard plugin watches.
#
Expand All @@ -130,7 +122,7 @@ def run_all
#
def run_on_modifications(paths)
::Guard::UI.info 'Guard::Edicy render'

puts "modifications: #{paths}"
paths.each do |path|
if path =~ /^(layouts|components)/
::Guard::UI.info "#{path} changed, rendering all pages"
Expand All @@ -143,21 +135,13 @@ def run_on_modifications(paths)
end
end

# Called on file(s) removals that the Guard plugin watches.
#
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_removals has failed
# @return [Object] the task result
#
# def run_on_removals(paths)
# puts 'YOYO run on removals'
# end
end

class Guuard

def initialize(renderer)
def initialize(renderer, filemanager)
@renderer = renderer
@filemanager = filemanager
end

def run
Expand All @@ -175,6 +159,7 @@ def run
::Guard.start(guardfile_contents: guardfile)
# ::Guard.start(guardfile_contents: guardfile)
::Guard.guards('yoyo').first.renderer = @renderer
::Guard.guards('yoyo').first.filemanager = @filemanager
end
end
end

0 comments on commit f058b3c

Please sign in to comment.