reivt 1.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.
data/lib/revit/cli.rb ADDED
@@ -0,0 +1,158 @@
1
+ # rubocop: disable Metrics/AbcSize, Metrics/MethodLength, Metrics/ClassLength
2
+ require 'paint'
3
+ require 'thor'
4
+ require 'tty-spinner'
5
+
6
+ #
7
+ # An extension of our main module
8
+ #
9
+ # @author [brwnrclse]
10
+ #
11
+ module Revit
12
+ #
13
+ # Rev's cli
14
+ #
15
+ # @author [brwnrclse]
16
+ #
17
+ class CLI < Thor
18
+ namespace :revit
19
+
20
+ map %w(--version -v) => :__print_version
21
+ desc '--version, -v', 'print the version'
22
+ def __print_version
23
+ puts VERSION
24
+ end
25
+
26
+ method_option :description, type: :string, aliases: '-d', lazy_default: '',
27
+ desc: 'A short summary of the changes'
28
+ method_option :list_files, type: :array, aliases: '-l',
29
+ desc: 'A list of files to add to the rev'
30
+ method_option :title, type: :string, aliases: '-t', lazy_default: '',
31
+ desc: 'A title for the rev'
32
+ desc 'create PATH', 'Make a new rev object from the parameters'
33
+ long_desc <<-LONGDESC
34
+ `Initiates a flow for creating a new rev. To bypass the prompts users can use the provided flags.
35
+ If using git the title, description and files will be retrieved from git via the git repo name, latest commit msg and latest commit file list respectively.
36
+
37
+ `revit create ./Bucket/repo` # Create a new rev from the provided path
38
+ `revit create ./Bucket/repo --title, -t "Test Repo"` # Create a new revo with a title of Test Repo
39
+ `revit create ./Bucket/repo --description, -d "A test repo."` # Create a new revo with a description of "a test repo"
40
+ `revit create ./Bucket/repo --list_files, -l ~/Bucket/oneoffs/help.js` # Create a new rev with a list of files, ignoring the current folder
41
+ LONGDESC
42
+ def create(path)
43
+ doc_set = Set.new
44
+ doc_ids = Set.new
45
+ paths = path.split
46
+ paths += options[:list_files] if options[:list_files]
47
+ rev_id = ''
48
+ spinner = TTY::Spinner.new('[:spinner] :msg', format: :bouncing_ball)
49
+
50
+ spinner.update(msg: 'Creating a rev...')
51
+ spinner.run do
52
+ rev_id = RevAPI.create_rev(options[:title], options[:description])
53
+ end
54
+ spinner.success(Paint['rev created', :green])
55
+
56
+ paths.each do |x|
57
+ type = File.exist?(x) ? File.ftype(x) : 'other'
58
+
59
+ if type == 'file'
60
+ spinner.update(msg: "#{Paint['Adding', nil, '#e3b505']} #{x}")
61
+ spinner.run do
62
+ doc_set.add(Util.doc_from_path(x))
63
+ end
64
+ spinner.success(Paint['document created', :green])
65
+ elsif type == 'directory'
66
+ spinner.update(msg: "#{Paint['Adding', nil, '#e3b505']} docs from " \
67
+ "#{Paint[path, '#2de1fc']}")
68
+
69
+ if Dir.entries(x).include?('.git')
70
+ spinner.run do
71
+ doc_set.merge(Util.docs_from_repo(x, spinner))
72
+ end
73
+ spinner.success(Paint['repo docs created', :green])
74
+ else
75
+ spinner.run do
76
+ doc_set.merge(Util.docs_from_dir(x))
77
+ end
78
+ spinner.success(Paint['dir docs created', :green])
79
+ end
80
+ else
81
+ spinner.error("#{Paint['Unsupported file type:']} #{path}")
82
+ end
83
+ end
84
+
85
+ spinner.update(msg: 'Uploading docs to rev api')
86
+ spinner.run do
87
+ doc_set.each do |doc|
88
+ doc_id = RevAPI.create_doc(doc.blob, doc.content_type, doc.doc_name,
89
+ doc.has_diff, rev_id)
90
+ doc_ids.add(doc_id)
91
+ end
92
+ end
93
+ spinner.success(Paint['docs uploaded to api', :green])
94
+
95
+ puts "\nLogin at #{Paint['wver.vaemoi.co/', :green]} to start your rev!\n"
96
+ rescue Errno::ECONNRESET, Errno::EINVAL, EOFError, Net::HTTPBadResponse,
97
+ Net::HTTPHeaderSyntaxError, Net::OpenTimeout, Net::ProtocolError,
98
+ Revit::BaemptyException, Revit::GraphQLDataException,
99
+ Revit::GraphQLValidationException => e
100
+
101
+ Revit::DEVLOGGER.error(e.message)
102
+ spinner.error(e.message)
103
+
104
+ doc_ids.each do |id|
105
+ RevAPI.delete_doc(id)
106
+ end
107
+
108
+ RevAPI.delete_rev(rev_id)
109
+ end
110
+
111
+ desc 'login', 'Get an auth token for accessing the rev api'
112
+ long_desc <<-LONGDESC
113
+ Sends a request for a valid auth token (jwt) to use for accessing the API.
114
+ Refresh is handled automatically as along as the user keeps on using rev otherwise it'll expire after about a week.
115
+
116
+ `rev login --username brwnrclse`
117
+
118
+ `rev login -u brwnrclse`
119
+ LONGDESC
120
+ def login
121
+ puts "Login here for your token:\n\n"
122
+ puts "#{Paint[Auth.auth_code_url, :green]}\n\n"
123
+
124
+ auth_code = Thor::Shell::Basic.new.ask('Enter your auth code => ')
125
+ spinner = TTY::Spinner.new('[:spinner] :msg', format: :bouncing_ball)
126
+
127
+ spinner.update(msg: 'Logging in')
128
+ spinner.run do
129
+ begin
130
+ auth_token = Auth.auth_token(auth_code)
131
+ user_id = RevAPI.signin_user(auth_token[:auth0_id])
132
+
133
+ if user_id.nil?
134
+ spinner.update(msg: 'User not found! Creating...')
135
+ user_id = RevAPI.create_user(auth_token[:auth0_id])
136
+ spinner.success(Paint['User created', :green])
137
+ end
138
+
139
+ Revit::REVIT_STORE.transaction do |store|
140
+ store[:access_token] = auth_token[:access_token].strip
141
+ store[:expires] = auth_token[:expires]
142
+ store[:auth0_id] = auth_token[:auth0_id].strip
143
+ store[:user_id] = user_id
144
+ end
145
+ spinner.success(Paint['Login successful :)', :green])
146
+ rescue Errno::ECONNRESET, Errno::EINVAL, EOFError,
147
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
148
+ Net::OpenTimeout, Net::ProtocolError,
149
+ Revit::GraphQLDataException,
150
+ Revit::GraphQLValidationException => e
151
+
152
+ Revit::DEVLOGGER.error(e.message)
153
+ spinner.error(Paint[e.message, :red])
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,67 @@
1
+ # An extension of our main module
2
+ #
3
+ # @author [brwnrclse]
4
+ #
5
+ module Revit
6
+ # Blueprint for Document objects
7
+ #
8
+ # @author [brwnrclse]
9
+ #
10
+ # @!attribute [rw] blob
11
+ # @return [String] The contents of the file
12
+ #
13
+ # @!attribute [rw] content_type
14
+ # @return [String] The type of content within the file
15
+ #
16
+ # @!attribute [rw] doc_name
17
+ # @return [Array<DocumentClass>] The name of the file
18
+ #
19
+ # @!attribute [rw] has_diff
20
+ # @return [Boolean] Whether or not the file will have a diff
21
+ #
22
+ class Document
23
+ attr_accessor(:blob, :content_type, :doc_name, :name, :has_diff)
24
+
25
+ def initialize(blob, content_type, doc_name, has_diff = false)
26
+ @blob = blob
27
+ @content_type = content_type
28
+ @has_diff = has_diff
29
+ @doc_name = doc_name
30
+ end
31
+
32
+ # Allows Documents objects to be compared using .eql? using their contents
33
+ # and file name.
34
+ #
35
+ # @param other [Rev::Document] The Document to compare to
36
+ #
37
+ # @return [Boolean] True if equal
38
+ # False if not equa
39
+ #
40
+ def eql?(other)
41
+ self == other
42
+ end
43
+
44
+ # Allows Documents objects to be compared using hashes created from a
45
+ # logical and (&) between the files contents and name.
46
+ #
47
+ # @param other [Rev::Document] The Document to compare to
48
+ #
49
+ # @return [Fixnum] The & between file content and name
50
+ #
51
+ def hash
52
+ @blob.hash & @doc_name.hash
53
+ end
54
+
55
+ # Allows Documents objects to be compared using == using their contents
56
+ # and file name.
57
+ #
58
+ # @param other [Rev::Document] The Document to compare to
59
+ #
60
+ # @return [Boolean] True if equal
61
+ # False if not equa
62
+ #
63
+ def ==(other)
64
+ @blob == other.blob && @doc_name == other.doc_name
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,35 @@
1
+ # An extension of our main module
2
+ #
3
+ # @author [brwnrclse]
4
+ #
5
+ module Revit
6
+ # Custom errors for dealing with repos in revit
7
+ #
8
+ # @author [bwrnrclse]
9
+ #
10
+ class BaemptyException < IOError
11
+ def initialize(msg = '')
12
+ super(msg)
13
+ end
14
+ end
15
+
16
+ # Custom errors for dealing with graphql in revit
17
+ #
18
+ # @author [bwrnrclse]
19
+ #
20
+ class GraphQLDataException < IOError
21
+ def initialize(msg = '')
22
+ super(msg)
23
+ end
24
+ end
25
+
26
+ # Custom errors for dealing with graphql in revit
27
+ #
28
+ # @author [bwrnrclse]
29
+ #
30
+ class GraphQLValidationException < IOError
31
+ def initialize(msg = '')
32
+ super(msg)
33
+ end
34
+ end
35
+ end