Class: Reviser::Components::Organiser
- Inherits:
-
Reviser::Component
- Object
- Reviser::Component
- Reviser::Components::Organiser
- Includes:
- Helpers::Project::Naming
- Defined in:
- lib/reviser/components/organiser.rb
Overview
Class which organizes all directories to simplify projects' analysis. Organiser renames projects folders and organises the whole of projects in order to have a structured folder (files at the root of folder) During this step, a git repository will be created, with an initial commit.
The other important task of Organiser is to detect all students, all binoms and all groups, thank to the directory name.
Constant Summary
Constants included from Helpers::Project::Naming
Helpers::Project::Naming::REGEX, Helpers::Project::Naming::SYMBOLS
Instance Method Summary collapse
-
#git(entry) ⇒ Object
Initializes a git repo.
-
#initialize(data) ⇒ Organiser
constructor
A new instance of Organiser.
-
#rename(entry) ⇒ Object
Renames directories more clearly.
-
#run ⇒ Object
Method which runs the organiser.
-
#structure(entry) ⇒ Object
Method which moves project's directories in order to have the same hierarchy for all project.
Methods included from Helpers::Project::Naming
#analyze_formatter, #ask, #check_entry_name, #format, #generate_label, #get_position, #sort_infos
Methods inherited from Reviser::Component
Constructor Details
#initialize(data) ⇒ Organiser
Returns a new instance of Organiser.
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 |
# File 'lib/reviser/components/organiser.rb', line 52 def initialize(data) raise ArgumentError if data == nil || !data.respond_to?('each') super data @directory = Cfg[:dest] @path = @directory @git = nil @students = [] @binoms = [] @projects_per_group = {} @unknown = [] @results = [] # How many patterns are in the pseudo-regex? @count_patterns = {} # Is git present ? if Cfg[:create_git_repo] require_gem 'git' require_relative '../helpers/git' self.class.send(:include, Helpers::Git) end end |
Instance Method Details
#git(entry) ⇒ Object
Initializes a git repo.
143 144 145 146 147 148 149 |
# File 'lib/reviser/components/organiser.rb', line 143 def git(entry) Dir.chdir File.join(@directory, entry) do git_init git_add git_commit end end |
#rename(entry) ⇒ Object
Renames directories more clearly.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/reviser/components/organiser.rb', line 80 def rename(entry) name = format entry if name != nil if name != entry new_path = File.join(@directory, name) FileUtils.mkdir_p(new_path.split(File.basename(new_path))[0]) FileUtils.mv(File.join(@directory, entry), new_path, :force => true) @logger.h2 Logger::INFO, "renaming #{File.basename(entry)} to #{File.basename(name)}" else @logger.h2 Logger::INFO, "#{entry} has not been renamed}, already formatted" end else @logger.h2 Logger::ERROR, "Can't rename #{File.basename(entry)} - Datas not found in name" end @results << name end |
#run ⇒ Object
Method which runs the organiser. It will apply all importants methods of this class for each project.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/reviser/components/organiser.rb', line 154 def run to_do = [:structure, :git, :rename] to_do.delete(:git) unless Cfg[:create_git_repo] i = 0 to_do.each do |method| puts "----[#{i+1}/#{to_do.size}] #{method.capitalize}" @logger.h1 Logger::INFO, "#{method.capitalize}" @data.each do |entry| send method, entry end i += 1 end @logger.h1 Logger::INFO, "#{@projects_per_group.keys.size} group#{'s' if @projects_per_group.keys.size > 1} have been detected" @logger.h1 Logger::INFO, "#{@students.size} student#{'s' if @students.size > 1} have been detected" @logger.h1 Logger::INFO, "#{@binoms.size} binom#{'s' if @binoms.size > 1} have been detected" formalized = [] @projects_per_group.each { |k,v| formalized << "#{k.to_s}: #{v} project#{'s' if v > 1}" } log_resume(formalized, Logger::INFO, "Groups:") log_resume(@students, Logger::INFO, "Students:") log_resume(@binoms, Logger::INFO, "Binoms:") log_resume(@unknown, Logger::ERROR, "\n#{@unknown.size} projects didn't matched with regex") @results end |
#structure(entry) ⇒ Object
Method which moves project's directories in order to have the same hierarchy for all project.
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 139 |
# File 'lib/reviser/components/organiser.rb', line 101 def structure(entry) chdir entry @logger.h2 Logger::INFO, "#{entry} => #{@path}" level = 0 @logger.h2 Logger::INFO, "Files in #{@path}" @logger.h3 Logger::INFO, "#{all}" @logger.h2 Logger::INFO, "Dirs in #{@path}" @logger.h3 Logger::INFO, "#{directories}" # directory to delete if the project directory is not structured rm = directories.first # Loop to find the core of project # # Basically running through each level of directories # while there is only one directory in the current directory. # Sometimes needed when archives have a sub-folder with their name. # In case the student created multiple nested folders, we don't # do anything. # while all == directories && directories.size == 1 level += 1 @logger.h2 Logger::DEBUG, "Level += 1\nPath = #{@path}" chdir directories.first @logger.h2 Logger::DEBUG, "New path = #{@path}" end # If the core of project is not at the root of directory ... if level >= 1 Dir.glob(File.join(@path,'*')).each do |file| FileUtils.mv(file,File.join(@directory, entry)) end @logger.h2 Logger::INFO, "Structuring #{File.join(@path)}" @logger.h2 Logger::INFO, "Removing #{File.join(@directory, entry, rm)}" FileUtils.rm_rf(File.join(@directory, entry, rm)) end @path = @directory end |