Class: SdocAll::Base

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

Direct Known Subclasses

Gems, Paths, Plugins, Rails, Ruby

Constant Summary collapse

BASE_PATH =
Pathname.new(Dir.pwd).expand_path
DOCS_PATH =
BASE_PATH + 'docs'
PUBLIC_PATH =
BASE_PATH + 'public'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/sdoc_all/base.rb', line 5

def config
  @config
end

Class Method Details

.add_merge_task(options = {}) ⇒ Object



128
129
130
# File 'lib/sdoc_all/base.rb', line 128

def add_merge_task(options = {})
  @@tasks << MergeTask.new(options)
end

.add_task(options = {}) ⇒ Object



124
125
126
# File 'lib/sdoc_all/base.rb', line 124

def add_task(options = {})
  @@tasks << Task.new(options)
end

.base_pathObject



51
52
53
# File 'lib/sdoc_all/base.rb', line 51

def base_path
  BASE_PATH
end

.chdir(path, &block) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/sdoc_all/base.rb', line 182

def chdir(path, &block)
  path = Pathname(path)
  dirs.push(path.expand_path)
  Dir.chdir(path, &block)
ensure
  dirs.pop
end

.clearObject



89
90
91
# File 'lib/sdoc_all/base.rb', line 89

def clear
  entries.clear
end

.dirsObject



178
179
180
# File 'lib/sdoc_all/base.rb', line 178

def dirs
  @@dirs ||= []
end

.docs_pathObject



55
56
57
# File 'lib/sdoc_all/base.rb', line 55

def docs_path
  DOCS_PATH.tap(&:mkpath)
end

.dry_run!Object



38
39
40
# File 'lib/sdoc_all/base.rb', line 38

def dry_run!
  @@dry_run = true
end

.dry_run?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sdoc_all/base.rb', line 41

def dry_run?
  defined?(@@dry_run) && @@dry_run
end

.entriesObject



85
86
87
# File 'lib/sdoc_all/base.rb', line 85

def entries
  @entries ||= []
end

.inherited(subclass) ⇒ Object



81
82
83
# File 'lib/sdoc_all/base.rb', line 81

def inherited(subclass)
  subclasses[subclass.short_name] = subclass
end

.output_for_verbose_level(n) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sdoc_all/base.rb', line 197

def output_for_verbose_level(n)
  if verbose_level >= n
    yield
  else
    old_stdout = $stdout
    old_stderr = $stderr
    dev_null = File.open('/dev/null', 'w')
    begin
      $stdout = dev_null
      $stderr = dev_null
      yield
    ensure
      $stdout = old_stdout
      $stderr = old_stderr
      dev_null.close
    end
  end
end

.public_pathObject



59
60
61
# File 'lib/sdoc_all/base.rb', line 59

def public_path
  PUBLIC_PATH
end

.remove_if_present(path) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/sdoc_all/base.rb', line 170

def remove_if_present(path)
  path = Pathname(path)
  if path.exist?
    puts "rm -r #{path.to_s.shellescape}".magenta
    FileUtils.remove_entry(path) unless dry_run?
  end
end

.short_nameObject



67
68
69
# File 'lib/sdoc_all/base.rb', line 67

def short_name
  name.demodulize.underscore
end

.sources_pathObject



71
72
73
74
75
# File 'lib/sdoc_all/base.rb', line 71

def sources_path
  Pathname.new("sources/#{short_name}").tap do |path|
    path.mkpath
  end
end

.subclassesObject



63
64
65
# File 'lib/sdoc_all/base.rb', line 63

def subclasses
  @subclasses ||= {}
end

.system(*args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sdoc_all/base.rb', line 132

def system(*args)
  args = args.map(&:to_s)
  command = args.length == 1 ? args.first : args.shelljoin
  if verbose_level >= 1
    puts [dirs.last && "cd #{dirs.last}", command].compact.join('; ').shrink(250).blue
  end
  unless dry_run?
    if verbose_level >= 2
      Kernel.system(*args)
    else
      rd, wr = IO::pipe

      pid = fork{
        rd.close
        STDOUT.reopen(wr)
        STDERR.reopen(wr)
        wr.close
        exec(*args)
      }

      wr.close
      begin
        true while line = rd.gets
      ensure
        rd.close unless rd.closed?
        Process.wait(pid)
      end
    end
    unless $?.success?
      if $?.signaled?
        raise SignalException.new($?.termsig)
      else
        abort("failed: #{command}")
      end
    end
  end
end

.tasks(options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sdoc_all/base.rb', line 104

def tasks(options = {})
  @@tasks = []
  entries.with_progress('configuring').each do |entry|
    entry.add_tasks(options)
  end
  subclasses.values.each do |subclass|
    unless subclass.used_sources.empty?
      paths = FileList.new
      paths.include(subclass.sources_path + '*')
      subclass.used_sources.each do |path|
        paths.exclude(path)
      end
      paths.resolve.each do |path|
        remove_if_present(path)
      end
    end
  end
  @@tasks
end

.to_document(type, config) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/sdoc_all/base.rb', line 93

def to_document(type, config)
  type = type.to_s
  config.symbolize_keys! if config.is_a?(Hash)
  subclass = subclasses[type] || subclasses[type.singularize] || subclasses[type.pluralize]
  if subclass
    entries << subclass.new(config)
  else
    raise ConfigError.new("don't know how to build \"#{type}\" => #{config.inspect}")
  end
end

.used_sourcesObject



77
78
79
# File 'lib/sdoc_all/base.rb', line 77

def used_sources
  @used_sources ||= []
end

.verbose_levelObject



47
48
49
# File 'lib/sdoc_all/base.rb', line 47

def verbose_level
  defined?(@@verbose_level) ? @@verbose_level : 0
end

.verbose_level=(val) ⇒ Object



44
45
46
# File 'lib/sdoc_all/base.rb', line 44

def verbose_level=(val)
  @@verbose_level = val.to_i
end

.with_env(key, value) ⇒ Object



190
191
192
193
194
195
# File 'lib/sdoc_all/base.rb', line 190

def with_env(key, value)
  old_value, ENV[key] = ENV[key], value
  yield
ensure
  ENV[key] = old_value
end