texas 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +15 -0
  2. data/bin/texas +1 -1
  3. data/lib/texas.rb +13 -2
  4. data/lib/texas/build.rb +32 -0
  5. data/lib/texas/build/base.rb +47 -24
  6. data/lib/texas/build/config.rb +118 -0
  7. data/lib/texas/build/config_loader.rb +93 -0
  8. data/lib/texas/build/dry.rb +1 -1
  9. data/lib/texas/build/final.rb +2 -2
  10. data/lib/texas/build/task/add_default_templates_to_build_path.rb +4 -0
  11. data/lib/texas/build/task/base.rb +2 -0
  12. data/lib/texas/build/task/copy_contents_to_build_path.rb +3 -0
  13. data/lib/texas/build/task/execute_after_scripts.rb +18 -0
  14. data/lib/texas/build/task/execute_before_scripts.rb +18 -0
  15. data/lib/texas/build/task/open_pdf.rb +7 -21
  16. data/lib/texas/build/task/publish_pdf.rb +35 -27
  17. data/lib/texas/build/task/run_master_template.rb +2 -0
  18. data/lib/texas/build/task/script.rb +28 -0
  19. data/lib/texas/core_ext.rb +2 -1
  20. data/lib/texas/core_ext/hash.rb +34 -0
  21. data/lib/texas/core_ext/string.rb +1 -128
  22. data/lib/texas/option_parser.rb +153 -124
  23. data/lib/texas/output_helper.rb +58 -0
  24. data/lib/texas/runner.rb +32 -51
  25. data/lib/texas/task/base.rb +1 -0
  26. data/lib/texas/task/new_project.rb +1 -1
  27. data/lib/texas/task/watch.rb +29 -18
  28. data/lib/texas/template.rb +6 -2
  29. data/lib/texas/template/helper/base.rb +60 -9
  30. data/lib/texas/template/helper/tex.rb +2 -4
  31. data/lib/texas/template/runner.rb +2 -5
  32. data/lib/texas/template/runner/base.rb +59 -11
  33. data/lib/texas/template/runner/md.rb +2 -2
  34. data/lib/texas/template/runner/tex.rb +2 -2
  35. data/lib/texas/template/template_error.rb +19 -5
  36. data/lib/texas/version.rb +1 -1
  37. data/spec/fixtures/basic-tex/contents/{unused_template.tex.erb → sub_dir/unused_template.tex.erb} +0 -0
  38. data/spec/fixtures/new-project/lib/init.rb +1 -1
  39. data/spec/fixtures/pdflatex-error/contents/contents.tex.erb +1 -0
  40. data/spec/spec_helper.rb +16 -7
  41. data/spec/texas/build/base_spec.rb +53 -9
  42. data/spec/texas/build/config_spec.rb +73 -0
  43. data/spec/texas/build/task/base_spec.rb +21 -0
  44. data/spec/texas/option_parser_spec.rb +53 -0
  45. data/spec/texas/task/base_spec.rb +49 -0
  46. data/spec/texas_spec.rb +49 -3
  47. metadata +76 -73
  48. data/lib/texas/build/task/run_before_scripts.rb +0 -22
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTExNmYwNjAzZGVhYmE0ZTMwMzkzNTc3NDY5ODQzMWM1ZjUyNWU4NQ==
5
+ data.tar.gz: !binary |-
6
+ YWVlNmFkNDhkNTI0YWQ5Yzg0ZTk2YTc3YTU4NWQ2ZmQ3NDFmYjkzYg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjQxNGM0N2ExZTQxOTU2MTI2OWFlZjQxZmZiYmQ0NmJlYzA1NWUwMGY3ODc1
10
+ ZmU3YTg5ODk5NzA1NWI2MWZiNTM3NTIxMTQ1NjYxMjM5NzdhNmJhMDJiMGYz
11
+ MmJiODQ3ZTczN2UyZjcyMDYyZDk0YTAwZGMwOWQ1NTZmNjIzNjU=
12
+ data.tar.gz: !binary |-
13
+ NGJmYzJiY2ZlYzE3ZWYxYWY3ZTEyNDAxNzY3NWIwMzc5Y2MzNTdmNzMyNjc3
14
+ NjZhM2ViZjdiODJmOWFmOGIzM2VkNDc4M2FjOWYwNDU2YmQ1YjhiODYyOTJh
15
+ ODE1MTM5MWE2OTAxNTU0Yjc4YmM0ZTcxNzgwODQ0Yzc4YWQ1NjI=
data/bin/texas CHANGED
@@ -7,4 +7,4 @@ rescue Bundler::GemfileNotFound => e
7
7
  require 'texas'
8
8
  end
9
9
 
10
- Texas::Runner.new
10
+ Texas::Runner.new(ARGV)
@@ -1,7 +1,17 @@
1
1
  module Texas
2
2
  class << self
3
- attr_accessor :contents_subdir_name, :texas_dir
4
- attr_accessor :verbose, :warnings
3
+ # Returns the name of the template sub directory
4
+ # defaults to "contents"
5
+ attr_accessor :contents_subdir_name
6
+
7
+ # Points to the directory where Texas is located
8
+ attr_accessor :texas_dir
9
+
10
+ # Returns true if Texas is run in verbose mode
11
+ attr_accessor :verbose
12
+
13
+ # Returns true if warnings are enabled
14
+ attr_accessor :warnings
5
15
  end
6
16
  end
7
17
  Texas.texas_dir = File.join(File.dirname(__FILE__), '..')
@@ -9,6 +19,7 @@ Texas.contents_subdir_name = "contents"
9
19
 
10
20
  require 'pp'
11
21
 
22
+ require_relative 'texas/output_helper'
12
23
  require_relative 'texas/option_parser'
13
24
  require_relative 'texas/build'
14
25
  require_relative 'texas/core_ext'
@@ -1,13 +1,45 @@
1
1
  module Texas
2
2
  module Build
3
+ class << self
4
+ include Texas::OutputHelper
5
+
6
+ # Display the error message that caused the exception.
7
+ #
8
+ def display_error_message(build, ex)
9
+ trace TraceInfo.new(:error, "#{build.options.task} aborted!", :red)
10
+ trace "\n" + ex.message
11
+ if build.options.backtrace
12
+ trace ex.backtrace
13
+ else
14
+ trace "(See full trace with --backtrace)"
15
+ end
16
+ end
17
+
18
+ # Run the given build object. If any errors occur, display them in a
19
+ # formatted way and call the optional block, if present.
20
+ #
21
+ def run_with_nice_errors(build, &block)
22
+ build.run
23
+ rescue Interrupt => ex
24
+ trace "\n"
25
+ trace TraceInfo.new("interrupt", "#{build.options.task} interrupted!", :yellow)
26
+ exit 0
27
+ rescue StandardError => ex
28
+ display_error_message(build, ex)
29
+ block.() if block
30
+ end
31
+ end
3
32
  end
4
33
  end
5
34
 
35
+ require_relative 'build/config'
36
+ require_relative 'build/config_loader'
6
37
  require_relative 'build/base'
7
38
  require_relative 'build/dry'
8
39
  require_relative 'build/final'
9
40
 
10
41
  require_relative 'build/task/base'
42
+ require_relative 'build/task/script'
11
43
 
12
44
  all_rbs = Dir[ File.join( File.dirname(__FILE__), "build", "task", "*.rb" ) ]
13
45
  all_rbs.each do |t|
@@ -3,12 +3,29 @@ require 'yaml'
3
3
  module Texas
4
4
  module Build
5
5
  class Base
6
+ include Texas::OutputHelper
6
7
  CONFIG_FILE = ".texasrc"
7
8
  MASTER_TEMPLATE = "master.tex"
8
9
 
9
- attr_reader :root, :options
10
- attr_reader :master_file, :contents_dir, :contents_template
11
- attr_reader :current_template, :ran_templates
10
+ # Returns the path of the current Texas project
11
+ attr_reader :root
12
+
13
+ # Returns the options of this build.
14
+ attr_reader :options
15
+
16
+ # Returns the location of the template that is run.
17
+ attr_reader :master_file
18
+
19
+ attr_reader :contents_dir
20
+
21
+ # Returns the name of the template whose contents should be rendered.
22
+ attr_reader :contents_template
23
+
24
+ # Returns the name of the template currently rendered.
25
+ attr_reader :current_template
26
+
27
+ # Returns an array of all templates that have been rendered.
28
+ attr_reader :ran_templates
12
29
 
13
30
  def initialize(_options)
14
31
  @options = _options
@@ -16,63 +33,69 @@ module Texas
16
33
  @contents_dir = options.contents_dir
17
34
  @contents_template = options.contents_template
18
35
  @master_file = File.join(__path__, MASTER_TEMPLATE)
19
-
20
- verbose { "Starting #{self.class}" }
21
- verbose { "[i] work_dir: #{options.work_dir}".dark }
22
- verbose { "[i] contents_dir: #{@contents_dir}".dark }
23
- verbose { "[i] contents_template: #{@contents_template}".dark }
24
- verbose { "[i] build_path: #{__path__}".dark }
36
+ verbose { verbose_info }
25
37
  end
26
38
 
39
+ # Returns the full path of the directory where the build is happening.
40
+ #
27
41
  def __path__
28
42
  File.join(root, 'tmp', 'build')
29
43
  end
30
44
 
45
+ # Returns an object that can store persistent information throughout
46
+ # the build process.
47
+ #
31
48
  def store
32
49
  @store ||= OpenStruct.new
33
50
  end
34
51
 
52
+ # Sets the currently rendered template.
53
+ #
35
54
  def current_template=(t)
36
55
  @ran_templates ||= []
37
56
  @ran_templates << t unless t.nil?
38
57
  @current_template = t
39
58
  end
40
59
 
41
- def document_struct
42
- @document_struct ||= begin
43
- hash = config["document"] || {}
44
- if options.merge_config
45
- hash.merge! config[options.merge_config]
46
- end
47
- OpenStruct.new(hash)
48
- end
49
- end
50
-
60
+ # Returns the Config object.
61
+ #
51
62
  def config
52
- @config ||= begin
53
- filename = File.join(root, CONFIG_FILE)
54
- File.exist?(filename) ? YAML.load_file(filename) : {}
55
- end
63
+ @config ||= Config.create ConfigLoader.new(root, CONFIG_FILE).to_hash, options.merge_config
56
64
  end
57
65
 
66
+ # Returns the location where the generated PDF file should be after the build.
67
+ #
58
68
  def dest_file
59
69
  @dest_file ||= File.join(root, "bin", "#{Template.basename contents_template}.pdf")
60
70
  end
61
71
 
72
+ # Runs the given build tasks.
73
+ #
62
74
  def run_build_tasks(*tasks)
63
75
  tasks.flatten.each { |t| run_build_task t }
64
76
  end
65
77
 
78
+ # Runs the given build task.
79
+ #
66
80
  def run_build_task(klass)
81
+ verbose { TraceInfo.new(:build_task, klass, :green) }
67
82
  klass = eval("::Texas::Build::Task::#{klass}") if [Symbol, String].include?(klass.class)
68
- verbose { "[b] #{klass}".dark }
69
83
  klass.new(self).run
70
84
  end
71
85
 
86
+ # Executes the build process.
87
+ #
72
88
  def run
73
89
  run_build_tasks before_tasks, basic_tasks, after_tasks
74
90
  end
75
91
 
92
+ def verbose_info
93
+ [
94
+ TraceInfo.new("work_dir", options.work_dir, :dark),
95
+ TraceInfo.new("build_path", __path__, :dark),
96
+ ].join("\n")
97
+ end
98
+
76
99
  %w(before basic after).each do |method|
77
100
  class_eval <<-INSTANCE_EVAL
78
101
  def #{method}_tasks
@@ -0,0 +1,118 @@
1
+ module Texas
2
+ module Build
3
+ # This class holds the config information for a build.
4
+ #
5
+ class Config
6
+ def initialize(hash)
7
+ @hash = hash.stringify_keys
8
+ end
9
+
10
+ def [](key)
11
+ @hash[key.to_s]
12
+ end
13
+
14
+ # Returns an object for the +document+ config key.
15
+ #
16
+ # Example:
17
+ # # .texasrc
18
+ #
19
+ # document:
20
+ # title: "My Document"
21
+ #
22
+ # config = Config.new YAML.load_file(".texasrc")
23
+ # # => #<Texas::Build::Config ...>
24
+ #
25
+ # config.document.title
26
+ # # => "My Document"
27
+ #
28
+ def document
29
+ @document ||= OpenStruct.new self[:document]
30
+ end
31
+
32
+ # Returns an object for the +document+ config key.
33
+ #
34
+ # Example:
35
+ # # .texasrc
36
+ #
37
+ # document:
38
+ # title: "My Document (DRAFT!)"
39
+ # final:
40
+ # document:
41
+ # title: "My Document"
42
+ #
43
+ # config = Config.new YAML.load_file(".texasrc")
44
+ # # => #<Texas::Build::Config ...>
45
+ #
46
+ # config.document.title
47
+ # # => "My Document (DRAFT!)"
48
+ #
49
+ # config.merge! :final
50
+ # # => #<Texas::Build::Config ...>
51
+ #
52
+ # config.document.title
53
+ # # => "My Document"
54
+ #
55
+ def merge!(key)
56
+ @document = nil
57
+ merge_hash = @hash[key.to_s]
58
+ if merge_hash
59
+ @hash.deep_merge! merge_hash.stringify_keys
60
+ self
61
+ else
62
+ raise "Trying to merge config with none existing key #{key.inspect}"
63
+ end
64
+ end
65
+
66
+ def method_missing(m, *args, &block)
67
+ self[m] || super
68
+ end
69
+
70
+ # Returns an element from the +script+ config key
71
+ #
72
+ # Example:
73
+ # # .texasrc
74
+ #
75
+ # script:
76
+ # before: "touch some_file"
77
+ # document:
78
+ # title: "My Document"
79
+ #
80
+ # config = Config.new YAML.load_file(".texasrc")
81
+ # # => #<Texas::Build::Config ...>
82
+ #
83
+ # config.script(:before)
84
+ # # => "touch some_file"
85
+ #
86
+ # config.script(:after)
87
+ # # => nil
88
+ #
89
+ def script(key)
90
+ hash = self[:script] || {}
91
+ hash[key.to_s]
92
+ end
93
+
94
+ # Returns a Config object.
95
+ #
96
+ # Example:
97
+ # # .texasrc
98
+ #
99
+ # document:
100
+ # title: "My Document (DRAFT!)"
101
+ # final:
102
+ # document:
103
+ # title: "My Document"
104
+ #
105
+ # config = Config.create YAML.load_file(".texasrc"), :final
106
+ # # => #<Texas::Build::Config ...>
107
+ #
108
+ # config.document.title
109
+ # # => "My Document"
110
+ #
111
+ def self.create(hash, merge_key = nil)
112
+ config = self.new hash
113
+ config.merge! merge_key if merge_key
114
+ config
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,93 @@
1
+ module Texas
2
+ module Build
3
+ # This class looks for a given filename in the current and all parent directories.
4
+ #
5
+ # # Given the following directory structure:
6
+ #
7
+ # ~/
8
+ # projects/
9
+ # some_texas_project/
10
+ # .texasrc
11
+ # .texasrc
12
+ #
13
+ # In the above case, it would find and load
14
+ # ~/.texasrc and ~/projects/some_texas_project/.texasrc
15
+ # with the latter one overriding settings in the former one.
16
+ #
17
+ class ConfigLoader
18
+ def initialize(start_dir, filename)
19
+ @start_dir = start_dir
20
+ @filename = filename
21
+ end
22
+
23
+ # Returns all found files with the given filename in the current and all parent directories.
24
+ #
25
+ # Example:
26
+ # # Given the following directory structure:
27
+ #
28
+ # ~/
29
+ # projects/
30
+ # some_texas_project/
31
+ # .texasrc
32
+ # .texasrc
33
+ #
34
+ # all_config_files
35
+ # # => ["~/.texasrc", "~/projects/some_texas_project/.texasrc"]
36
+ #
37
+ def all_config_files
38
+ found_files = []
39
+ each_parent_dir(@start_dir) do |dir|
40
+ filename = File.join(dir, @filename)
41
+ found_files.unshift filename if File.exist?(filename)
42
+ end
43
+ found_files
44
+ end
45
+
46
+ def each_parent_dir(dir)
47
+ old_length = nil
48
+ while dir != '.' && dir.length != old_length
49
+ yield dir
50
+ old_length = dir.length
51
+ dir = File.dirname(dir)
52
+ end
53
+ end
54
+
55
+ # Returns a hash of all the found config files.
56
+ #
57
+ # Example:
58
+ # # Given the following directory structure:
59
+ #
60
+ # ~/
61
+ # projects/
62
+ # some_texas_project/
63
+ # .texasrc
64
+ # .texasrc
65
+ #
66
+ # # ~/.texasrc
67
+ #
68
+ # document:
69
+ # author: "John Doe"
70
+ # some_value: 42
71
+ #
72
+ # # ~/projects/some_texas_project/.texasrc
73
+ #
74
+ # document:
75
+ # title: "My Document"
76
+ # some_value: 123
77
+ #
78
+ # to_hash
79
+ # # => {:document => {
80
+ # :author => "John Doe",
81
+ # :title => "My Document",
82
+ # :some_value => 123}}
83
+ #
84
+ def to_hash
85
+ hash = {}
86
+ all_config_files.each do |filename|
87
+ hash.deep_merge! YAML.load_file(filename)
88
+ end
89
+ hash
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,7 +1,7 @@
1
1
  module Texas
2
2
  module Build
3
3
  class Dry < Base
4
- before_task :RunBeforeScripts, :CopyContentsToBuildPath
4
+ before_task :ExecuteBeforeScripts, :CopyContentsToBuildPath
5
5
 
6
6
  basic_task :AddDefaultTemplatesToBuildPath, :RunMasterTemplate
7
7
 
@@ -1,7 +1,7 @@
1
1
  module Texas
2
2
  module Build
3
3
  class Final < Dry
4
- append_after_task :PublishPDF, :OpenPDF
4
+ append_after_task :PublishPDF, :ExecuteAfterScripts, :OpenPDF
5
5
  end
6
6
  end
7
- end
7
+ end