Method: Taft.install

Defined in:
lib/taft.rb

.install(lang = nil, debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object



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
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
# File 'lib/taft.rb', line 34

def self.install(lang = nil, debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "")
    $TAFT_DEBUGGING = true if debugging

    unless dest.empty?
        puts "Taft.install has been called with the following parameters :"
        puts "Language             : #{lang}"
        puts "Project name         : #{project_name}"
        puts "Project abbreviation : #{project_abbrev}"
        puts "Destination folder (relative to #{Dir.getwd} if a relative path) : #{dest}"
        puts "Press Enter to continue..."
        gets
    end

    base_wd = Dir.getwd
    dest_base_folder = ""

    if lang == nil
        puts "\nPlease enter the language of this project : #{LANGUAGES.join(", ")}"
        lang = gets.chomp.downcase.to_sym
        raise "TAFT cannot install a new project in that language" unless LANGUAGES.include?(lang)
    end

    if dest.empty?
        puts "\nPlease enter the path of the base folder that TAFT should create, up to and including the name of the base folder"
        puts "TAFT will create this folder if it does not exist"
        puts "If a relative path is entered, it will be taken as being relative to path : #{Dir.getwd}"
        dest = gets.chomp
        raise "The base folder path entered was empty" if dest.empty?

        puts "Does this folder already exist? Entering Y will grant TAFT permission to write into that folder, overwriting any files with matching names that are aready present."
        folder_exists = gets.chomp

        overwrite_ok = (folder_exists == "Y")
    end
    # if dest has been specified in the call, then overwrite_ok has as well

    case lang
    when :ruby
        raw_file_path = "#{File.expand_path(File.dirname(__FILE__))}/taft_files"
        # TODO determine list of gems & put in folder
        # Might be better to output a list of gem install commands instead? What if people are using earlier/later versions of Ruby?
        # bundled_gem_path = "#{File.expand_path(File.dirname(__FILE__))}/bundled_gems"
        # install_gems(bundled_gem_path)
    when :java
        raw_file_path = "#{File.expand_path(File.dirname(__FILE__))}/java_taft_files"
    end

    # TODO does this handle dests like "~/foo" ?
    if (Pathname.new dest).absolute?
        dest_base_folder = dest
    else
        dest_base_folder = File.join(Dir.getwd, dest)
    end
    dest_base_folder = File.expand_path(dest_base_folder)
    puts "TAFT will install to #{dest_base_folder}"

    raise "Folder #{dest_base_folder} already exists, and you did not grant TAFT permission to write into the folder" if Dir.exists?(dest_base_folder) && !overwrite_ok

    # Create the base folder
    begin
        Dir.mkdir(dest_base_folder) unless Dir.exists?(dest_base_folder)
    ensure
        raise "The base folder '#{dest_base_folder}' did not exist" if folder_exists && !Dir.exists?(dest_base_folder)
        raise "The base folder '#{dest_base_folder}' could not be created" unless Dir.exists?(dest_base_folder)
    end

    # Copy the raw files into the base folder, preserving the structure
    FileUtils.copy_entry(raw_file_path, dest_base_folder, false, false, true)

    puts "\nFiles have been copied"
    if project_name.empty?
        puts "\nPlease enter the name of your project (e.g. RED SKY) :"
        project_name = gets.chomp
        raise "The project name entered was empty" if project_name.empty?
    end

    if project_abbrev.empty?
        puts "\nPlease enter the abbreviation of your project (e.g. RS) :"
        project_abbrev = gets.chomp
        raise "The project abbreviation entered was empty" if project_abbrev.empty?
    end


    @project_name_part = project_name.gsub(/[\s-]+/, "_").downcase # TODO use .snakecase ?
    @project_name_uppercase_part = @project_name_part.upcase
    @project_abbrev_part = project_abbrev.gsub(/[\s-]+/, "_").delete("_").downcase # TODO use .snakecase ?
    @project_abbrev_uppercase_part = @project_abbrev_part.upcase

    # Now sweep over the copied files, adjusting the names accordingly
    Taft.adjust_file_names(dest_base_folder, project_name, project_abbrev)

    # Now sweep over the copied files, adjusting the contents accordingly
    Taft.adjust_file_contents(dest_base_folder, project_name, project_abbrev)

    puts "\nFiles have been tailored to your project."

    puts "\nTAFT has installed a tailored copy of the #{lang.capitalize} Automation Framework to #{dest_base_folder}"
    puts "Installation complete."
end