require "sbag/version" require 'thor' require "colorize" require "MDownloader" require "fileutils" module Sbag class C < Thor #New File Command =====================================>>> desc "newfile", "Create a new file. Usage: console newfile [file_name].[file_extension]" method_option :newfile, :aliases => "-c" , :desc => "Use newfile command to create new file." def newfile(file) new_file = File.new("#{file}","w+") puts "New file created => *#{file}*".green end #========================END===============================# #Delete File Command =====================================>>> desc "delfile", "Delete the existing file. Usage: console delfile [file_name].[file_extension]" method_option :delfile, :aliases => "-d", :desc => "Use delfile command to delete existing file." def delfile(file) File.delete("#{file}") puts "Existing file deleted => *#{file}*".green end #========================END===============================# #Time Display Command =====================================>>> desc "time" , "Display current time. Usage: console time" method_option :time, :aliases => "-t", :desc => "Use time command to display the current time." def time time = Time.now puts "Current time => *#{time}*".green end #========================END===============================# #Current Location Command =====================================>>> desc "locate", "Current directory or folder path. Usage: console locate" method_option :locate, :aliases => "-loc", :desc => "Use locate command to find the current directory or folder path." def locate location = Dir.pwd puts "Current Location => *#{location}*".green end #========================END===============================# #Make new Directory Command =====================================>>> desc "newdir", "Create a new directory or folder. Usage: console newdir [dir_name]" method_option :newdir, :aliases => "-nd", :desc => "Use newdir command to create a new directory or folder in the current path." def newdir(new_dir) Dir.mkdir("#{new_dir}") location = Dir.pwd puts "New Directory created => *#{new_dir}* at *#{location}*".green end #========================END===============================# #Directory Listing Command =====================================>>> desc "list", "Files and directory Listing Command. Usage: console list" method_option :list, :aliases => "-list", :desc => "Use entries command to list all the files and directories in current path." def list location = Dir.pwd puts "#{location}".green end #========================END===============================# #Delete Directory Command =====================================>>> desc "deldir", "Delete directory or folder Command. Usage: console deldir [dir_name]" method_option :deldir, :aliases => "-ddir", :desc => "Use deldir command to delete the folder and directory in current path." def deldir(del_dir) Dir.delete("#{del_dir}") puts "Deleted directory => *#{del_dir}*".green end #========================END===============================# # start command Starts===================================>>> desc "start" ,"Initialize empty Kit depository in the current project folder. Usage: console start" method_option :start, :aliases => "-start" , :desc => "Use kit init command to make a directory of content in Kit depository." def start(new_depository = 'kit') if Dir.exist?('kit') == true puts "Depository *Kit* already exists!!! Please use add command to continue working on your project. Start command can be used only once.".red else Dir.mkdir("#{new_depository}") location = Dir.pwd puts "Depository *Kit* intialized for your project at *#{location}*".green end end # init command Ends===================================>>> # clone command Starts=================================>>> desc "clone", "Copy the project on which you want to work. Usage: console clone [url]" method_option :clone , :alises => "-clone" , :desc => "Use the kit clone command with URL of the project you want to copy." def clone(url) array_url = url.split('/') puts array_url.last location = Dir.pwd path = location.to_s + "/#{array_url.last}" options = Hash.new options[:retry] = :any #retry times, :any(any times retry), 0(no retry), or custom numbers(int) options[:resume] = true #true or false, resume continue download, break point resume download result, error = MDownloader.download(url, path, options) do |report| puts "Progress:#{report[:percent]}% Cloning:#{report[:min]} #{url} #{report[:sec]}" end if result puts 'Cloning Finished!' else puts 'Error in cloning #{url}' puts error end end # clone command Ends=====================================>>> # add command Starts=====================================>>> desc "add" ,"Add file contents to your project before you can make them. Usage : add [version number]" method_option :add , :alises => "-add", :desc => "Use the kit add command to intially add the file to your current project, but even if the file is already tracked" def add(version) source = FileUtils.pwd() dir_last = source.split("/") current_dir = dir_last.last structure_data = Dir["**/*"] data = Dir["*"] data.delete("kit") puts "-------- Process for adding Kit started --------" puts "Checking current depository files ==============>" puts structure_data puts "-------- Checking completed --------" if Dir.exist?("kit") == false puts "Kit depository not found!!! Use start command first.".red else Dir.chdir "kit" changed_dir = FileUtils.pwd() if Dir.exist?("v#{version}") == true puts "Kit *v#{version}* already exists!!! Please increment the version." else Dir.mkdir("v#{version}") destination = changed_dir + "/v#{version}" Dir.chdir(destination) structure_file = "structure.txt" File.new("#{structure_file}", "w+") File.open("#{structure_file}","w") do |f| structure_data.each do |ch| f.write("#{ch}\n") end end puts "Structure.txt file created successfully".yellow Dir.chdir(source) puts "Adding Kit for your Depository with v#{version} completed successfully.".green FileUtils.cp_r data, destination end end end # add command Ends=======================================>>> # status command Starts====================================>>>> desc "status" , "Status checking command. Usage: status [version_no] [file_name]" method_option :status, :aliases => "-status", :desc => "Use this command to compare the two files" def status(version_no, file_name) puts "Comparing file is #{file_name} in v#{version_no}".yellow #Current_file_path current_file_path = FileUtils.pwd() #Switch into kit folder Dir.chdir "kit" #Reading the file system.txt system_file_data = File.new("system.txt","r") project_dir_name = system_file_data.sysread(100) puts project_dir_name #Go back to previous directory previous_dir = File.expand_path("../.", Dir.pwd) Dir.chdir "#{previous_dir}" puts FileUtils.pwd() #Procedure for going into old kit version directory version = "kit/v#{version_no}/" Dir.chdir("#{version}") puts "You are in v#{version_no} of your project".yellow old_file_path = FileUtils.pwd() puts old_file_path puts Diffy::Diff.new(old_file_path , current_file_path , :source => 'files', :include_diff_info => true, :allow_empty_diff => false, :context => 10000000000000000).to_s(:color) end # status command Ends====================================>>> end end