Class: Fresco::CLI::New

Inherits:
Object
  • Object
show all
Defined in:
lib/fresco/cli/new.rb

Constant Summary collapse

USAGE =
<<~USAGE
  Usage: fresco new <app_name> [--fresco-path <dir>]

    --fresco-path <dir>   Generate a Gemfile pinned to a path-installed
                          fresco (instead of the rubygems version). Useful
                          during co-development of fresco itself.

  Example:
    fresco new bookshelf
    fresco new bookshelf --fresco-path ../fresco
USAGE

Instance Method Summary collapse

Instance Method Details

#run(argv) ⇒ Object



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
53
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fresco/cli/new.rb', line 21

def run(argv)
  app_name = nil
  fresco_path = nil
  i = 0
  while i < argv.length
    a = argv[i]
    if a == "--fresco-path"
      fresco_path = argv[i + 1]
      unless fresco_path
        warn "fresco: --fresco-path requires a directory argument"
        return 1
      end
      i += 2
    elsif a.start_with?("-")
      warn "fresco: unknown flag #{a.inspect}"
      warn USAGE
      return 1
    else
      if app_name
        warn "fresco: positional <app_name> already set to #{app_name.inspect}"
        return 1
      end
      app_name = a
      i += 1
    end
  end

  if app_name.nil? || app_name.empty?
    warn USAGE
    return 1
  end

  unless app_name.match?(/\A[a-z][a-z0-9_]*\z/)
    warn "fresco: app name #{app_name.inspect} must be lowercase letters/digits/underscores starting with a letter"
    return 1
  end

  target = File.expand_path(app_name)
  if File.exist?(target)
    warn "fresco: #{target} already exists"
    return 1
  end

  locals = {
    app_name:      app_name,
    module_name:   camelize(app_name),
    fresco_source: fresco_path ? ", path: #{File.expand_path(fresco_path).inspect}" : ", source: \"https://rubygems.org\"",
  }

  FileUtils.mkdir_p(target)
  scaffold_root = Fresco::Paths.generator_root

  copied = 0
  Dir.glob("#{scaffold_root}/**/*", File::FNM_DOTMATCH).sort.each do |src|
    next if src.end_with?("/.", "/..")
    rel = src.sub(/\A#{Regexp.escape(scaffold_root)}\/?/, "")
    next if rel.empty?
    dst = File.join(target, strip_template_suffix(rel))

    if File.directory?(src)
      FileUtils.mkdir_p(dst)
      next
    end

    FileUtils.mkdir_p(File.dirname(dst))
    if src.end_with?(".tt")
      File.write(dst, ERB.new(File.read(src), trim_mode: "-").result_with_hash(locals))
    else
      FileUtils.cp(src, dst)
    end

    # Preserve executable bit for bin/* scripts (cp doesn't strip it,
    # but ERB-rendered .tt files re-create with default 0644). The
    # generator tree doesn't currently put .tt files under bin/, but
    # chmod here makes the rule uniform.
    File.chmod(0o755, dst) if rel.start_with?("bin/")
    copied += 1
  end

  puts "Created #{app_name}/ (#{copied} files)"
  puts ""
  puts "    cd #{app_name}"
  puts "    bundle install"
  puts "    bin/dev"
  puts ""
  0
end