Class: Kompo::CopyProjectFiles

Inherits:
Taski::Task
  • Object
show all
Defined in:
lib/kompo/tasks/copy_project_files.rb

Overview

Copy project files (entrypoint and additional files) to working directory

Instance Method Summary collapse

Instance Method Details

#cleanObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kompo/tasks/copy_project_files.rb', line 74

def clean
  work_dir = WorkDir.path
  return unless work_dir && Dir.exist?(work_dir)

  real_work_dir = File.realpath(work_dir)
  files = Taski.args.fetch(:files, [])

  # Clean up copied files (with path validation)
  if @entrypoint_path
    expanded_path = File.expand_path(@entrypoint_path)
    FileUtils.rm_f(@entrypoint_path) if expanded_path.start_with?(real_work_dir + File::SEPARATOR)
  end

  files.each do |file|
    path = File.join(work_dir, file)
    expanded_path = File.expand_path(path)
    # Only delete if path is inside work_dir
    FileUtils.rm_rf(path) if expanded_path.start_with?(real_work_dir + File::SEPARATOR) && File.exist?(path)
  end
  puts "Cleaned up project files"
end

#runObject



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kompo/tasks/copy_project_files.rb', line 10

def run
  work_dir = WorkDir.path
  project_dir = Taski.args.fetch(:project_dir, Taski.env.working_directory) || Taski.env.working_directory
  entrypoint = Taski.args.fetch(:entrypoint, "main.rb")
  files = Taski.args.fetch(:files, [])

  # Copy entrypoint (preserve relative path structure)
  src_entrypoint = File.expand_path(File.join(project_dir, entrypoint))
  raise "Entrypoint not found: #{src_entrypoint}" unless File.exist?(src_entrypoint)

  # Validate source is inside project_dir
  real_project_dir = File.realpath(project_dir)
  real_src = File.realpath(src_entrypoint)
  unless real_src.start_with?(real_project_dir + File::SEPARATOR) || real_src == real_project_dir
    raise "Entrypoint path escapes project directory: #{entrypoint}"
  end

  @entrypoint_path = File.join(work_dir, entrypoint)
  FileUtils.mkdir_p(File.dirname(@entrypoint_path))
  FileUtils.cp(src_entrypoint, @entrypoint_path)
  puts "Copied entrypoint: #{entrypoint}"

  # Copy additional files/directories
  @additional_paths = []
  files.each do |file|
    src = File.expand_path(File.join(project_dir, file))
    next unless File.exist?(src)

    # Validate source is inside project_dir
    real_src = File.realpath(src)
    unless real_src.start_with?(real_project_dir + File::SEPARATOR) || real_src == real_project_dir
      warn "Skipping path that escapes project directory: #{file}"
      next
    end

    dest = File.join(work_dir, file)

    # Validate destination is inside work_dir
    real_work_dir = File.realpath(work_dir)
    expanded_dest = File.expand_path(dest)
    unless expanded_dest.start_with?(real_work_dir + File::SEPARATOR) || expanded_dest == real_work_dir
      warn "Skipping path that would escape work directory: #{file}"
      next
    end

    if File.directory?(src)
      # Special handling for "." - copy directory contents directly to work_dir
      if file == "." || real_src == real_project_dir
        copy_directory_contents(src, work_dir)
        @additional_paths << work_dir
      else
        FileUtils.mkdir_p(dest)
        FileUtils.cp_r(src, File.dirname(dest))
        @additional_paths << dest
      end
    else
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(src, dest)
      @additional_paths << dest
    end
    puts "Copied: #{file}"
  end
end