Class: Sakusei::StylePack
- Inherits:
-
Object
- Object
- Sakusei::StylePack
- Defined in:
- lib/sakusei/style_pack.rb
Constant Summary collapse
- STYLE_PACKS_DIR =
'style_packs'- SAKUSEI_DIR =
'.sakusei'- SAKUSEI_CONFIG =
'config.yml'
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#footer ⇒ Object
readonly
Returns the value of attribute footer.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#stylesheet ⇒ Object
readonly
Returns the value of attribute stylesheet.
Class Method Summary collapse
-
.base_stylesheet ⇒ Object
Path to the base CSS that is always applied before style pack CSS.
-
.discover(start_dir, requested_name = nil) ⇒ Object
Discover style pack by walking up the directory tree.
-
.extract_component_info(file, pack_name = nil) ⇒ Object
Extract full component information from a Vue file.
- .extract_docs_description(file) ⇒ Object
-
.find_component(start_dir, component_name) ⇒ Object
Find a component by name across style packs and local directories.
- .find_sakusei_dir(start_dir) ⇒ Object
-
.init(directory, name) ⇒ Object
Initialize a new style pack.
-
.list_available(start_dir = '.') ⇒ Object
List all available style packs.
- .load_from_path(packs_dir, requested_name) ⇒ Object
- .read_config(sakusei_dir) ⇒ Object
-
.set_default(start_dir, style_name) ⇒ Object
Set the default style pack in the nearest .sakusei/config.yml.
- .write_config(sakusei_dir, data) ⇒ Object
Instance Method Summary collapse
- #components_dir ⇒ Object
-
#extract_component_info(file) ⇒ Object
Instance method wrapper for extract_component_info.
-
#initialize(path, name = nil) ⇒ StylePack
constructor
A new instance of StylePack.
- #list_components ⇒ Object
Constructor Details
#initialize(path, name = nil) ⇒ StylePack
Returns a new instance of StylePack.
19 20 21 22 23 |
# File 'lib/sakusei/style_pack.rb', line 19 def initialize(path, name = nil) @path = path @name = name || File.basename(path) load_files end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/sakusei/style_pack.rb', line 12 def config @config end |
#footer ⇒ Object (readonly)
Returns the value of attribute footer.
12 13 14 |
# File 'lib/sakusei/style_pack.rb', line 12 def @footer end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
12 13 14 |
# File 'lib/sakusei/style_pack.rb', line 12 def header @header end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/sakusei/style_pack.rb', line 12 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/sakusei/style_pack.rb', line 12 def path @path end |
#stylesheet ⇒ Object (readonly)
Returns the value of attribute stylesheet.
12 13 14 |
# File 'lib/sakusei/style_pack.rb', line 12 def stylesheet @stylesheet end |
Class Method Details
.base_stylesheet ⇒ Object
Path to the base CSS that is always applied before style pack CSS
15 16 17 |
# File 'lib/sakusei/style_pack.rb', line 15 def self.base_stylesheet File.('../templates/base.css', __dir__) end |
.discover(start_dir, requested_name = nil) ⇒ Object
Discover style pack by walking up the directory tree
26 27 28 29 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 |
# File 'lib/sakusei/style_pack.rb', line 26 def self.discover(start_dir, requested_name = nil) sakusei_path = find_sakusei_dir(start_dir) # Resolve name: explicit arg takes priority, then config default resolved_name = requested_name resolved_name ||= read_config(sakusei_path)['default_style'] if sakusei_path if resolved_name # Built-in 'default' pack requested explicitly — skip to fallback below unless resolved_name == 'default' # Search across ALL .sakusei dirs in the tree (not just the nearest), # so a named pack in a parent dir is found even if a child .sakusei # directory exists but has no style_packs/ subdirectory. pack_entry = list_available(start_dir).find { |p| p[:name] == resolved_name } raise Error, "Style pack '#{resolved_name}' not found. Run 'sakusei styles' to see available packs." unless pack_entry return new(pack_entry[:path], pack_entry[:name]) end else # No name at all: use the first pack from the nearest .sakusei if sakusei_path packs_dir = File.join(sakusei_path, STYLE_PACKS_DIR) return load_from_path(packs_dir, nil) if Dir.exist?(packs_dir) end end # Fall back to built-in default style pack default_path = File.('../templates/default_style_pack', __dir__) new(default_path, 'default') end |
.extract_component_info(file, pack_name = nil) ⇒ Object
Extract full component information from a Vue file
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 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sakusei/style_pack.rb', line 140 def self.extract_component_info(file, pack_name = nil) pack_name ||= File.basename(File.dirname(File.dirname(file))) content = File.read(file) # Extract docs section docs_match = content.match(/<docs>(.+?)<\/docs>/m) docs = docs_match ? docs_match[1].strip : nil # Extract template section template_match = content.match(/<template>(.+?)<\/template>/m) template = template_match ? template_match[1].strip : nil # Extract script section script_match = content.match(/<script(?:\s+setup)?>(.+?)<\/script>/m) script = script_match ? script_match[1].strip : nil # Extract style section style_match = content.match(/<style(?:\s+scoped)?>(.+?)<\/style>/m) style = style_match ? style_match[1].strip : nil # Parse props from script props = parse_props(script) if script # Generate usage example usage = generate_usage(File.basename(file, '.vue'), props, template) { name: File.basename(file, '.vue'), description: docs ? docs.lines.first&.strip : nil, full_description: docs, path: file, pack_name: pack_name, template: template, script: script, style: style, props: props, usage: usage } end |
.extract_docs_description(file) ⇒ Object
99 100 101 102 103 |
# File 'lib/sakusei/style_pack.rb', line 99 def self.extract_docs_description(file) content = File.read(file) match = content.match(/<docs>\s*\n\s*(.+)/) match ? match[1].strip : nil end |
.find_component(start_dir, component_name) ⇒ Object
Find a component by name across style packs and local directories
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 137 |
# File 'lib/sakusei/style_pack.rb', line 106 def self.find_component(start_dir, component_name) # Search in style packs sakusei_path = find_sakusei_dir(start_dir) if sakusei_path packs_dir = File.join(sakusei_path, STYLE_PACKS_DIR) if Dir.exist?(packs_dir) Dir.glob(File.join(packs_dir, '*')).select { |f| File.directory?(f) }.each do |pack_path| component_file = File.join(pack_path, 'components', "#{component_name}.vue") if File.exist?(component_file) pack = new(pack_path) return pack.extract_component_info(component_file) end end end end # Search in local ./components directory local_component = File.join(Dir.pwd, 'components', "#{component_name}.vue") if File.exist?(local_component) return extract_component_info(local_component, 'local') end # Search in default style pack default_path = File.('../templates/default_style_pack', __dir__) default_component = File.join(default_path, 'components', "#{component_name}.vue") if File.exist?(default_component) pack = new(default_path, 'default') return pack.extract_component_info(default_component) end nil end |
.find_sakusei_dir(start_dir) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/sakusei/style_pack.rb', line 293 def self.find_sakusei_dir(start_dir) current = File.(start_dir) loop do sakusei_path = File.join(current, SAKUSEI_DIR) return sakusei_path if Dir.exist?(sakusei_path) parent = File.dirname(current) break if parent == current # Reached root current = parent end nil end |
.init(directory, name) ⇒ Object
Initialize a new style pack
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sakusei/style_pack.rb', line 70 def self.init(directory, name) sakusei_path = File.join(directory, SAKUSEI_DIR) pack_path = File.join(sakusei_path, STYLE_PACKS_DIR, name) FileUtils.mkdir_p(pack_path) # Copy default templates default_path = File.('../templates/default_style_pack', __dir__) FileUtils.cp_r(Dir.glob("#{default_path}/*"), pack_path) pack_path end |
.list_available(start_dir = '.') ⇒ Object
List all available style packs
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/sakusei/style_pack.rb', line 237 def self.list_available(start_dir = '.') packs = [] nearest_sakusei_path = nil # Find all .sakusei directories walking up from start_dir current = File.(start_dir) visited_dirs = Set.new loop do sakusei_path = File.join(current, SAKUSEI_DIR) if Dir.exist?(sakusei_path) && !visited_dirs.include?(sakusei_path) nearest_sakusei_path ||= sakusei_path visited_dirs.add(sakusei_path) packs_dir = File.join(sakusei_path, STYLE_PACKS_DIR) if Dir.exist?(packs_dir) Dir.glob(File.join(packs_dir, '*')).select { |f| File.directory?(f) }.each do |pack_path| packs << { name: File.basename(pack_path), path: pack_path } end end end parent = File.dirname(current) break if parent == current current = parent end # Add default style pack default_path = File.('../templates/default_style_pack', __dir__) packs << { name: 'default', path: default_path } # Remove duplicates by name (closer packs take precedence) seen_names = Set.new unique_packs = packs.select { |p| seen_names.add?(p[:name]) } # Mark whichever pack is set as default in config default_name = nearest_sakusei_path ? read_config(nearest_sakusei_path)['default_style'] : nil unique_packs.map { |p| p.merge(default: p[:name] == default_name) } end |
.load_from_path(packs_dir, requested_name) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/sakusei/style_pack.rb', line 309 def self.load_from_path(packs_dir, requested_name) available_packs = Dir.glob(File.join(packs_dir, '*')).select { |f| File.directory?(f) } raise Error, "No style packs found in #{packs_dir}" if available_packs.empty? if requested_name pack_path = available_packs.find { |p| File.basename(p) == requested_name } raise Error, "Style pack '#{requested_name}' not found" unless pack_path elsif available_packs.length == 1 pack_path = available_packs.first else # Interactive selection would happen here # For now, use the first one pack_path = available_packs.first end new(pack_path) end |
.read_config(sakusei_dir) ⇒ Object
279 280 281 282 283 284 285 |
# File 'lib/sakusei/style_pack.rb', line 279 def self.read_config(sakusei_dir) config_path = File.join(sakusei_dir, SAKUSEI_CONFIG) return {} unless File.exist?(config_path) YAML.safe_load(File.read(config_path)) || {} rescue Psych::Exception {} end |
.set_default(start_dir, style_name) ⇒ Object
Set the default style pack in the nearest .sakusei/config.yml
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sakusei/style_pack.rb', line 57 def self.set_default(start_dir, style_name) sakusei_path = find_sakusei_dir(start_dir) raise Error, "No .sakusei directory found. Run 'sakusei init' to create a style pack first." unless sakusei_path available = list_available(start_dir) unless available.any? { |p| p[:name] == style_name } raise Error, "Style pack '#{style_name}' not found. Run 'sakusei styles' to see available packs." end write_config(sakusei_path, 'default_style' => style_name) end |
.write_config(sakusei_dir, data) ⇒ Object
287 288 289 290 291 |
# File 'lib/sakusei/style_pack.rb', line 287 def self.write_config(sakusei_dir, data) config_path = File.join(sakusei_dir, SAKUSEI_CONFIG) existing = read_config(sakusei_dir) File.write(config_path, YAML.dump(existing.merge(data))) end |
Instance Method Details
#components_dir ⇒ Object
83 84 85 86 |
# File 'lib/sakusei/style_pack.rb', line 83 def components_dir dir = File.join(@path, 'components') Dir.exist?(dir) ? dir : nil end |
#extract_component_info(file) ⇒ Object
Instance method wrapper for extract_component_info
181 182 183 |
# File 'lib/sakusei/style_pack.rb', line 181 def extract_component_info(file) self.class.extract_component_info(file, @name) end |
#list_components ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sakusei/style_pack.rb', line 88 def list_components return [] unless components_dir Dir.glob(File.join(components_dir, '*.vue')).sort.map do |file| { name: File.basename(file, '.vue'), description: self.class.extract_docs_description(file), path: file } end end |