Module: DocOpsLab::Dev::ScriptManager

Defined in:
lib/docopslab/dev/script_manager.rb

Class Method Summary collapse

Class Method Details

.list_script_templatesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/docopslab/dev/script_manager.rb', line 54

def list_script_templates
  scripts_source = Library.resolve('scripts')
  unless scripts_source && Dir.exist?(scripts_source)
    puts '❌ scripts not found in library; run `labdev:sync:library` to fetch.'
    return false
  end

  puts '📜 Available script templates:'

  Dir.glob("#{scripts_source}/*").each do |script_path|
    next unless File.file?(script_path)

    script_name = File.basename(script_path)

    # Try to extract description from script comments
    description = 'No description available'
    if File.readable?(script_path)
      File.open(script_path, 'r') do |f|
        f.each_line do |line|
          if line.match(/^#\s*(.+)$/) && !line.include?('!/bin/')
            description = line.match(/^#\s*(.+)$/)[1]
            break
          end
        end
      end
    end

    puts "#{script_name}: #{description}"
  end

  true
end

.run_script(script_name, args = []) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
138
# File 'lib/docopslab/dev/script_manager.rb', line 87

def run_script script_name, args=[]
  unless script_name
    puts '❌ Script name is required'
    puts 'Usage: bundle exec rake labdev:run[script_name] -- [args]'
    return false
  end

  # Add .sh extension if NO extension provided
  # (valid extensions are .sh, .rb, py, .js)
  script_name += '.sh' unless File.extname(script_name).length.positive?

  # Look for local script first
  project_script = File.join('scripts', script_name)
  vendor_script = File.join('scripts', '.vendor', 'docopslab', script_name)

  script_to_run = nil
  if File.exist?(project_script)
    puts "📜 Running local script: #{project_script}"
    script_to_run = project_script
  elsif File.exist?(vendor_script)
    puts "📜 Running vendor script: #{vendor_script}"
    script_to_run = vendor_script
  else
    puts "❌ Script not found: #{script_name}"
    puts 'Searched in:'
    puts "  - #{project_script}"
    puts "  - #{vendor_script}"
    return false
  end

  # Run the script using proper runtime and args
  case File.extname(script_to_run)
  when '.sh', ''
    cmd = ['bash']
  when '.rb'
    cmd = ['ruby']
  when '.py'
    cmd = ['python3']
  when '.js'
    cmd = ['node']
  else
    puts "❌ Unsupported script extension: #{File.extname(script_to_run)}"
    return false
  end
  cmd << script_to_run
  cmd.concat(args) if args.any?
  puts "🚀 Executing: #{cmd.join(' ')}"

  system(*cmd)

  $CHILD_STATUS.success?
end

.sync_scriptsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/docopslab/dev/script_manager.rb', line 9

def sync_scripts
  puts '📜 Syncing common scripts from DocOps Lab...'

  unless Library.available?
    puts '❌ Library not available; run `labdev:sync:library` to fetch.'
    return false
  end
  scripts_source = Library.resolve('scripts')
  unless scripts_source && Dir.exist?(scripts_source)
    puts '❌ scripts not found in library; run `labdev:sync:library` to fetch.'
    return false
  end

  # Ensure vendor scripts directory exists
  vendor_scripts_dir = File.join('scripts', '.vendor', 'docopslab')
  FileUtils.mkdir_p(vendor_scripts_dir)

  synced_count = 0

  Dir.glob("#{scripts_source}/*").each do |script_path|
    next unless File.file?(script_path)

    script_name = File.basename(script_path)
    dest_path = File.join(vendor_scripts_dir, script_name)

    # Check if file needs updating
    if !File.exist?(dest_path) || File.read(script_path) != File.read(dest_path)
      FileUtils.cp(script_path, dest_path)
      File.chmod(0o755, dest_path) # Make executable
      puts "  📜 Synced: #{dest_path} (executable)"
      synced_count += 1
    else
      puts "  ✅ Up to date: #{dest_path}"
    end
  end

  if synced_count.positive?
    puts "✅ Script sync complete; #{synced_count} files updated"
  else
    puts '✅ All scripts up to date'
  end

  true
end