organisir 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0dadc67c7d01274914cd72e80465d626bd301156413aaab76e1a300d6c90441
4
+ data.tar.gz: d7f6c8029b92de538bbf3047a157f7baff018193a591ce7342a15ad89b6ecff5
5
+ SHA512:
6
+ metadata.gz: dc07f03a7b247ad434d7212c295edaa9cb242288bd5cbae9951060a180618b08dbc752a2e814f88a75e38c8868be778fc9053a104cf03145a4eb90678f2f03c6
7
+ data.tar.gz: beb27b3f0db78af1cf7786da39d65c59f15998d0a7fda9f78e460c9e02f0d58865e00bfc0ec94e7d7758afe6ffd6405558b512a87c3ac5d9728dfeb24dc87d78
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.6.5
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.0.rc.2
17
+ bundle install
18
+ bundle exec rake
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
@@ -0,0 +1,13 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Documentation:
13
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in organisir.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 0.80"
11
+
12
+ gem "thor", "~> 1.0"
13
+
14
+ gem "colorize", "~> 0.8.1"
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ organisir (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ coderay (1.1.3)
11
+ colorize (0.8.1)
12
+ method_source (1.0.0)
13
+ minitest (5.14.2)
14
+ optparse (0.1.0)
15
+ parallel (1.20.1)
16
+ parser (2.7.2.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.0.0)
22
+ rake (13.0.1)
23
+ regexp_parser (2.0.0)
24
+ rexml (3.2.4)
25
+ rubocop (0.93.1)
26
+ parallel (~> 1.10)
27
+ parser (>= 2.7.1.5)
28
+ rainbow (>= 2.2.2, < 4.0)
29
+ regexp_parser (>= 1.8)
30
+ rexml
31
+ rubocop-ast (>= 0.6.0)
32
+ ruby-progressbar (~> 1.7)
33
+ unicode-display_width (>= 1.4.0, < 2.0)
34
+ rubocop-ast (1.2.0)
35
+ parser (>= 2.7.1.5)
36
+ ruby-progressbar (1.10.1)
37
+ thor (1.0.1)
38
+ unicode-display_width (1.7.0)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ colorize (~> 0.8.1)
45
+ minitest (~> 5.0)
46
+ optparse (~> 0.1.0)
47
+ organisir!
48
+ pry (~> 0.13.1)
49
+ rake (~> 13.0)
50
+ rubocop (~> 0.80)
51
+ thor (~> 1.0)
52
+
53
+ BUNDLED WITH
54
+ 2.2.0.rc.2
@@ -0,0 +1,36 @@
1
+ # Organisir
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/organisir`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'organisir'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install organisir
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/organisir.
36
+
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
6
+
7
+ require "thor"
8
+ require "organisir"
9
+
10
+ Organisir::Cli.start ARGV
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "thor"
5
+ require "colorize"
6
+
7
+
8
+ module Organisir
9
+ end
10
+
11
+ # require "organisir/"
12
+ require "organisir/cli"
13
+ require "organisir/client"
14
+ require "organisir/file_op"
15
+ require "organisir/rule"
16
+ require "organisir/symlink_rule"
17
+ require "organisir/validator"
18
+ require "organisir/version"
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ module Organisir
6
+ class Cli < Thor
7
+ def self.exit_on_failure?
8
+ true
9
+ end
10
+
11
+ COMMANDS = {
12
+ start: %w[
13
+ Move files from a source directory to a destination directory based on the filename of all files inside
14
+ the source directory. Only files present in the source directory will be scanned and moved. The destination
15
+ directory should ideally contain multiple directories where you would want your files to move.
16
+ ].join(" "),
17
+ organise: "Alias of start",
18
+ symlink: %w[
19
+ Scan all directories inside a directory and the files contained in each of them (upto one level). Each file
20
+ which matches multiple rules will have a symlink created in each directory where the rule was matched. The
21
+ original file will not be moved and new symlinks will refer to the original file path.
22
+ ].join(" "),
23
+ clean: %w[
24
+ Remove all dangling symlinks from a directory by scanning through all the directories inside the given
25
+ source directory
26
+ ].join(" "),
27
+ version: "Display installed tmuxinator version"
28
+ }.freeze
29
+
30
+ desc "start source destination", "Move files from source to destination directory"
31
+ option :source, aliases: :s, required: true, type: :string
32
+ option :destination, aliases: :d, required: true, type: :string
33
+ option :commit, type: :boolean, default: false
34
+
35
+ def start
36
+ if options[:commit]
37
+ say "Preparing to move files in 5 seconds. Cancel the command by pressing Ctrl-C"
38
+ # sleep(5)
39
+ else
40
+ say "Start command always runs in non-commit mode. Use --commit true to permanently move files.", :red
41
+ end
42
+ pwd = Dir.pwd
43
+ Organisir::Client.new.start(options[:source], options[:destination], pwd, options[:commit])
44
+ rescue Interrupt
45
+ say "Exiting organisir...", :green
46
+ end
47
+
48
+ desc "source", "Symlink files in nested directories to matching sub-directories within the given directory"
49
+ option :source, aliases: :s, required: true, type: :string
50
+ option :commit, type: :boolean, default: false
51
+ def symlink
52
+ if options[:commit]
53
+ say "Preparing to symlink files in 5 seconds. Cancel the command by pressing Ctrl-C"
54
+ # sleep(5)
55
+ else
56
+ say "Symlink command always runs in non-commit mode. Use --commit true to symlink files.", :red
57
+ end
58
+ pwd = Dir.pwd
59
+ Organisir::Client.new.symlink(options[:source], pwd, options[:commit])
60
+ rescue Interrupt
61
+ say "Exiting organisir...", :green
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Organisir
4
+ class Client
5
+ def start(source_dir, dest_dir, pwd, commit)
6
+ @abs_source_dir = File.join(pwd, source_dir)
7
+ @abs_dest_dir = File.join(pwd, dest_dir)
8
+ Validator.validate_start(@abs_source_dir, @abs_dest_dir)
9
+
10
+ @verbose = !commit
11
+ @pwd = pwd
12
+ @file_op = FileOp.new(Dir.pwd)
13
+ execute_start
14
+ end
15
+
16
+ def symlink(source_dir, pwd, commit)
17
+ @abs_source_dir = File.join(pwd, source_dir)
18
+ @verbose = !commit
19
+ @pwd = pwd
20
+ @file_op = FileOp.new(Dir.pwd)
21
+
22
+ execute_symlink
23
+ end
24
+
25
+ private
26
+
27
+ def execute_start
28
+ sub_dirs = get_sub_dirs(@abs_dest_dir)
29
+ # print sub_dirs
30
+ print("Scanned #{sub_dirs.length} directories in #{@abs_dest_dir}\n")
31
+ src_files = get_src_files(@abs_source_dir)
32
+ print("Scanned #{src_files.length} files in #{@abs_source_dir}\n")
33
+
34
+ rule = Rule.new(sub_dirs)
35
+ src_files.each do |f|
36
+ match_dir = rule.match(f)
37
+ next if match_dir.nil?
38
+
39
+ move_file(f, match_dir)
40
+ end
41
+ end
42
+
43
+ def execute_symlink
44
+ sub_dirs = get_sub_dirs(@abs_source_dir)
45
+ files = Dir.glob(File.join(@abs_source_dir, "*", "*.*"))
46
+ .reject { |f| File.directory?(f) || File.symlink?(f) || File.zero?(f) }
47
+ print("Scanned #{files.length} files inside #{sub_dirs.length} directories in #{@abs_dest_dir}\n")
48
+
49
+ rule = SymlinkRule.new(sub_dirs)
50
+ files.each do |f|
51
+ match_dirs = rule.match(f)
52
+ next if match_dirs.nil? || match_dirs.empty?
53
+
54
+ symlink_file(f, match_dirs)
55
+ end
56
+ end
57
+
58
+ def move_file(filename, dirname)
59
+ print "Rule matched file #{filename.to_s.colorize(:red)} to be moved directory #{dirname.to_s.colorize(:red)}\n"
60
+ return if @verbose
61
+
62
+ @file_op.move(filename, @abs_source_dir, File.join(@abs_dest_dir, dirname))
63
+ end
64
+
65
+ def symlink_file(file, match_dirs)
66
+ print "Rule matched file #{file.to_s.colorize(:red)} to be symlinked to directories #{match_dirs.join(", ").to_s.colorize(:red)}\n"
67
+ return if @verbose
68
+
69
+ @file_op.symlink(file, match_dirs)
70
+ end
71
+
72
+ def get_sub_dirs(dir)
73
+ Dir.chdir(dir)
74
+ sub_dirs = Dir["*"].select { |o| File.directory?(o) }
75
+ Dir.chdir(@pwd)
76
+ sub_dirs
77
+ end
78
+
79
+ def get_src_files(dir)
80
+ Dir.entries(dir).reject { |f| File.directory? f }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module Organisir
6
+ class FileOp
7
+ def initialize(root_dir)
8
+ @root_dir = root_dir
9
+ end
10
+
11
+ def move(filename, source_path, destination_path)
12
+ src = File.join(source_path, filename)
13
+ dest = File.join(destination_path, filename)
14
+ # print "mv#{false ? ' -f' : ''} #{[src,dest].flatten.join ' '}\n".colorize(:green)
15
+ FileUtils.move(src, dest, force: false, verbose: false)
16
+ end
17
+
18
+ def symlink(src_file, destination_dirs)
19
+ filename = src_file.split("/").last
20
+ destns = destination_dirs.map do |p|
21
+ File.join(@root_dir, p, filename)
22
+ end
23
+
24
+ destns.each do |d|
25
+ FileUtils.symlink(src_file, d, force: false, verbose: true)
26
+ end
27
+ end
28
+
29
+ def clean(link, path)
30
+ src = File.join(@root_dir, path, link)
31
+ # Last explicit check to see if the file indeed is a symlink
32
+ return unless File.symlink?(src)
33
+
34
+ FileUtils.remove(src)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Organisir
4
+ class Rule
5
+ def initialize(dirs)
6
+ @dirs = dirs
7
+ @dir_map = {}
8
+ @dirs.map do |dir|
9
+ @dir_map[dir] = gen_regex_for_dir_name(dir)
10
+ end
11
+ end
12
+
13
+ def match(file)
14
+ resp = []
15
+ @dir_map.keys.each do |k|
16
+ resp << k if file.downcase.match(@dir_map[k])
17
+ end
18
+
19
+ resp.min unless resp.empty?
20
+ end
21
+
22
+ private
23
+
24
+ def gen_regex_for_dir_name(dir)
25
+ tokens = dir.downcase.split
26
+ name_token = tokens.join('\\b.*\\b')
27
+ Regexp.new("\\b#{name_token}\\b", Regexp::EXTENDED | Regexp::IGNORECASE)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Organisir
4
+ class SymlinkRule
5
+ def initialize(dirs)
6
+ @dirs = dirs
7
+ @dir_map = {}
8
+ @dirs.map do |dir|
9
+ @dir_map[dir] = gen_regex_for_dir_name(dir)
10
+ end
11
+ end
12
+
13
+ def match(file)
14
+ resp = []
15
+ filename = file.split("/").last.downcase
16
+ file_dir = file.split("/").fetch(-2)
17
+ @dirs.each do |d|
18
+ next if file_dir.include? d
19
+
20
+ resp << d if filename.downcase.match? @dir_map[d]
21
+ end
22
+ resp
23
+ end
24
+
25
+ private
26
+
27
+ def gen_regex_for_dir_name(dir)
28
+ tokens = dir.downcase.split
29
+ name_token = tokens.join('\\b.*\\b')
30
+ Regexp.new("\\b#{name_token}\\b", Regexp::EXTENDED | Regexp::IGNORECASE)
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Organisir
4
+ class Validator
5
+ class << self
6
+ def validate_start(source_dir, dest_dir)
7
+ raise "#{source_dir} is not a valid directory path" unless File.directory?(source_dir)
8
+
9
+ raise "#{dest_dir} is not a valid directory path" unless File.directory?(dest_dir)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Organisir
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/organisir/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "organisir"
7
+ spec.version = Organisir::VERSION
8
+ spec.authors = ["anujchandra"]
9
+ spec.email = ["anujc4@gmail.com"]
10
+
11
+ spec.summary = "Gem to organise files based on their filename"
12
+ spec.description = "Gem to organise files based on their filename"
13
+ spec.homepage = "https://github.com/anujc4/organisir"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/anujc4/organisir"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ # spec.files = Dir["lib/**/*.rb"]
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: organisir
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - anujchandra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem to organise files based on their filename
14
+ email:
15
+ - anujc4@gmail.com
16
+ executables:
17
+ - organisir
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/main.yml"
22
+ - ".gitignore"
23
+ - ".rubocop.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README.md
27
+ - Rakefile
28
+ - bin/setup
29
+ - exe/organisir
30
+ - lib/organisir.rb
31
+ - lib/organisir/cli.rb
32
+ - lib/organisir/client.rb
33
+ - lib/organisir/file_op.rb
34
+ - lib/organisir/rule.rb
35
+ - lib/organisir/symlink_rule.rb
36
+ - lib/organisir/validator.rb
37
+ - lib/organisir/version.rb
38
+ - organisir.gemspec
39
+ homepage: https://github.com/anujc4/organisir
40
+ licenses: []
41
+ metadata:
42
+ allowed_push_host: https://rubygems.org
43
+ homepage_uri: https://github.com/anujc4/organisir
44
+ source_code_uri: https://github.com/anujc4/organisir
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.5.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.8
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Gem to organise files based on their filename
64
+ test_files: []