1
1
use crate :: config:: geode_root;
2
- use crate :: util:: logging:: ask_value;
3
2
use std:: fs;
4
3
use std:: path:: PathBuf ;
5
- use git2:: { Repository , ResetType , IndexAddOption , Signature } ;
6
- use crate :: package:: mod_json_from_archive;
7
- use crate :: { info, done, fatal, warn, NiceUnwrap } ;
4
+ use crate :: fatal;
8
5
use colored:: Colorize ;
9
6
10
- fn reset_and_commit ( repo : & Repository , msg : & str ) {
11
- let head = repo. head ( ) . nice_unwrap ( "Broken repository, can't get HEAD" ) ;
12
- if !head. is_branch ( ) {
13
- fatal ! ( "Broken repository, detached HEAD" ) ;
14
- }
15
-
16
- let mut commit = head. peel_to_commit ( ) . unwrap ( ) ;
17
- while commit. parent_count ( ) > 0 {
18
- commit = commit. parent ( 0 ) . unwrap ( ) ;
19
- }
20
-
21
- repo. reset ( commit. as_object ( ) , ResetType :: Soft , None ) . nice_unwrap ( "Unable to refresh repository" ) ;
22
-
23
- let mut index = repo. index ( ) . nice_unwrap ( "cannot get the Index file" ) ;
24
- index. add_all ( [ "." ] . iter ( ) , IndexAddOption :: DEFAULT , None ) . nice_unwrap ( "Unable to add changes" ) ;
25
- index. write ( ) . nice_unwrap ( "Unable to write changes" ) ;
26
-
27
- let sig =
Signature :: now ( "GeodeBot" , "[email protected] " ) . unwrap ( ) ;
28
-
29
- let tree = repo. find_tree ( index. write_tree ( ) . nice_unwrap ( "Unable to get write tree" ) ) . unwrap ( ) ;
30
- repo. commit ( Some ( "HEAD" ) , & sig, & sig, msg, & tree, & [ & commit] ) . nice_unwrap ( "Unable to commit" ) ;
31
- }
32
-
33
7
pub fn indexer_path ( ) -> PathBuf {
34
8
geode_root ( ) . join ( "indexer" )
35
9
}
@@ -38,31 +12,6 @@ pub fn is_initialized() -> bool {
38
12
indexer_path ( ) . exists ( )
39
13
}
40
14
41
- pub fn initialize ( ) {
42
- if is_initialized ( ) {
43
- done ! ( "Indexer is already initialized" ) ;
44
- return ;
45
- }
46
-
47
- info ! (
48
- "Before publishing mods on the Geode index, we need to make you a local \
49
- Indexer, which handles everything related to publishing mods."
50
- ) ;
51
- info ! (
52
- "The mod binaries will be hosted on your Indexer repository, which will \
53
- automatically request them to be added to the official Mods Index."
54
- ) ;
55
- info ! (
56
- "To get started, log in to Github using your account, and go to \
57
- https://github.com/geode-sdk/indexer/fork to make a fork of the Indexer."
58
- ) ;
59
-
60
- let fork_url = ask_value ( "Enter the URL of your fork" , None , true ) ;
61
- Repository :: clone ( & fork_url, indexer_path ( ) ) . nice_unwrap ( "Unable to clone your repository." ) ;
62
-
63
- done ! ( "Successfully initialized Indexer" ) ;
64
- }
65
-
66
15
pub fn list_mods ( ) {
67
16
if !is_initialized ( ) {
68
17
fatal ! ( "Indexer has not been set up - use `geode indexer init` to set it up" ) ;
@@ -78,102 +27,3 @@ pub fn list_mods() {
78
27
}
79
28
}
80
29
}
81
-
82
- pub fn remove_mod ( id : String ) {
83
- if !is_initialized ( ) {
84
- fatal ! ( "Indexer has not been set up - use `geode indexer init` to set it up" ) ;
85
- }
86
- let indexer_path = indexer_path ( ) ;
87
-
88
- let mod_path = indexer_path. join ( & id) ;
89
- if !mod_path. exists ( ) {
90
- fatal ! ( "Cannot remove mod {}: does not exist" , id) ;
91
- }
92
-
93
- fs:: remove_dir_all ( mod_path) . nice_unwrap ( "Unable to remove mod" ) ;
94
-
95
- let repo = Repository :: open ( & indexer_path) . nice_unwrap ( "Unable to open repository" ) ;
96
- reset_and_commit ( & repo, & format ! ( "Remove {}" , & id) ) ;
97
-
98
- done ! ( "Succesfully removed {}\n " , id) ;
99
- info ! ( "You will need to force-push to sync your changes." ) ;
100
- info ! ( "Run `git -C {} push -f` to sync your changes" , indexer_path. to_str( ) . unwrap( ) ) ;
101
- }
102
-
103
- pub fn add_mod ( package : PathBuf ) {
104
- if !is_initialized ( ) {
105
- fatal ! ( "Indexer has not been set up - use `geode indexer init` to set it up" ) ;
106
- }
107
- let indexer_path = indexer_path ( ) ;
108
-
109
- if !package. exists ( ) {
110
- fatal ! ( "Package path {} does not exist!" , package. display( ) ) ;
111
- }
112
-
113
- let mut archive = zip:: ZipArchive :: new ( fs:: File :: open ( & package) . unwrap ( ) ) . nice_unwrap ( "Unable to read package" ) ;
114
-
115
- let mod_json = mod_json_from_archive ( & mut archive) ;
116
-
117
- let major_version = mod_json
118
- . get ( "version" )
119
- . nice_unwrap ( "[mod.json]: Missing key 'version'" )
120
- . as_str ( )
121
- . nice_unwrap ( "[mod.json].version: Expected string" )
122
- . split ( '.' )
123
- . next ( )
124
- . unwrap ( )
125
- . chars ( )
126
- . filter ( |x| * x != 'v' )
127
- . collect :: < String > ( ) ;
128
-
129
- let mod_id = mod_json_from_archive ( & mut archive)
130
- . get ( "id" )
131
- . nice_unwrap ( "[mod.json]: Missing key 'id'" )
132
- . as_str ( )
133
- . nice_unwrap ( "[mod.json].id: Expected string" )
134
- . to_string ( ) ;
135
-
136
- let mod_path = indexer_path. join ( format ! ( "{}@{}" , & mod_id, & major_version) ) ;
137
- if !mod_path. exists ( ) {
138
- fs:: create_dir ( & mod_path)
139
- . nice_unwrap ( "Unable to create directory in local indexer for mod" ) ;
140
- }
141
-
142
- fs:: copy ( package, mod_path. join ( "mod.geode" ) )
143
- . nice_unwrap ( "Unable to copy .geode package to local Indexer" ) ;
144
-
145
- let repo = Repository :: open ( & indexer_path)
146
- . nice_unwrap ( "Unable to open local Indexer repository" ) ;
147
- reset_and_commit ( & repo, & format ! ( "Add/Update {}" , & mod_id) ) ;
148
-
149
- match repo. find_remote ( "origin" ) . and_then ( |mut o| o. push ( & [ "main" ] , None ) ) {
150
- Ok ( _) => {
151
- done ! (
152
- "Succesfully added {}@{} to your indexer!" ,
153
- mod_id, major_version
154
- ) ;
155
- } ,
156
- Err ( _) => {
157
- done ! ( "Successfully added {}@{}\n " , mod_id, major_version) ;
158
- warn ! (
159
- "Unable to automatically sync the changes to Github. \
160
- You will need to push this commit yourself."
161
- ) ;
162
- info ! ( "Run `git -C {} push -f` to push the commit" , indexer_path. to_str( ) . unwrap( ) ) ;
163
- } ,
164
- }
165
- if let Some ( url) = repo. find_remote ( "origin" ) . unwrap ( ) . url ( ) {
166
- info ! (
167
- "To let us know you're ready to publish your mod, please open \
168
- a Pull Request on your repository: \
169
- {}/compare/geode-sdk:indexer:main...main",
170
- url
171
- ) ;
172
- }
173
- else {
174
- info ! (
175
- "To let us know you're ready to publish your mod, please open \
176
- a Pull Request on your Indexer fork repository."
177
- ) ;
178
- } ;
179
- }
0 commit comments