Class: Sakusei::StylePack

Inherits:
Object
  • Object
show all
Defined in:
lib/sakusei/style_pack.rb

Constant Summary collapse

STYLE_PACKS_DIR =
'style_packs'
SAKUSEI_DIR =
'.sakusei'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name = nil) ⇒ StylePack

Returns a new instance of StylePack.



17
18
19
20
21
# File 'lib/sakusei/style_pack.rb', line 17

def initialize(path, name = nil)
  @path = path
  @name = name || File.basename(path)
  load_files
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/sakusei/style_pack.rb', line 10

def config
  @config
end

Returns the value of attribute footer.



10
11
12
# File 'lib/sakusei/style_pack.rb', line 10

def footer
  @footer
end

#headerObject (readonly)

Returns the value of attribute header.



10
11
12
# File 'lib/sakusei/style_pack.rb', line 10

def header
  @header
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/sakusei/style_pack.rb', line 10

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/sakusei/style_pack.rb', line 10

def path
  @path
end

#stylesheetObject (readonly)

Returns the value of attribute stylesheet.



10
11
12
# File 'lib/sakusei/style_pack.rb', line 10

def stylesheet
  @stylesheet
end

Class Method Details

.base_stylesheetObject

Path to the base CSS that is always applied before style pack CSS



13
14
15
# File 'lib/sakusei/style_pack.rb', line 13

def self.base_stylesheet
  File.expand_path('../templates/base.css', __dir__)
end

.discover(start_dir, requested_name = nil) ⇒ Object

Discover style pack by walking up the directory tree



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sakusei/style_pack.rb', line 24

def self.discover(start_dir, requested_name = nil)
  sakusei_path = find_sakusei_dir(start_dir)

  if sakusei_path
    packs_dir = File.join(sakusei_path, STYLE_PACKS_DIR)
    return load_from_path(packs_dir, requested_name) if Dir.exist?(packs_dir)
  end

  # Fall back to default style pack
  default_path = File.expand_path('../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



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
138
139
140
141
142
143
144
145
146
# File 'lib/sakusei/style_pack.rb', line 108

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



67
68
69
70
71
# File 'lib/sakusei/style_pack.rb', line 67

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sakusei/style_pack.rb', line 74

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.expand_path('../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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/sakusei/style_pack.rb', line 241

def self.find_sakusei_dir(start_dir)
  current = File.expand_path(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



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sakusei/style_pack.rb', line 38

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.expand_path('../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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/sakusei/style_pack.rb', line 205

def self.list_available(start_dir = '.')
  packs = []

  # Find all .sakusei directories walking up from start_dir
  current = File.expand_path(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)
      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.expand_path('../templates/default_style_pack', __dir__)
  packs << { name: 'default', path: default_path }

  # Remove duplicates by name (closer packs take precedence)
  seen_names = Set.new
  packs.select { |p| seen_names.add?(p[:name]) }
end

.load_from_path(packs_dir, requested_name) ⇒ Object

Raises:



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/sakusei/style_pack.rb', line 257

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

Instance Method Details

#components_dirObject



51
52
53
54
# File 'lib/sakusei/style_pack.rb', line 51

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



149
150
151
# File 'lib/sakusei/style_pack.rb', line 149

def extract_component_info(file)
  self.class.extract_component_info(file, @name)
end

#list_componentsObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/sakusei/style_pack.rb', line 56

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