labt 0.3.9

Lab-t Lightweight Android build tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use crate::{
    config::{LabToml, Project},
    templating::{Activity, ActivityXml, AndroidManifest, StringsRes},
};
use anyhow::{bail, Context};
use clap::Args;
use dialoguer::{theme::ColorfulTheme, Input};
use regex::Regex;
use sailfish::TemplateOnce;
use std::{
    fs::{self, File},
    io::{self, Write},
    path::{Path, PathBuf},
};

use super::Submodule;

#[derive(Args, Clone)]
pub struct InitArgs {
    /// Project name
    #[arg(short, long, required_if_eq("no_interactive", "true"))]
    name: Option<String>,
    /// Java package name
    #[arg(long, required_if_eq("no_interactive", "true"))]
    package: Option<String>,
    /// Disable interactive mode
    #[arg(short = 'I', action)]
    no_interactive: bool,
    /// Directory to create project in
    path: Option<PathBuf>,
    #[arg(long, required_if_eq("no_interactive", "true"))]
    /// Internal version number
    version_number: Option<i32>,
    #[arg(long, required_if_eq("no_interactive", "true"))]
    /// External version name visible to users
    version_name: Option<String>,
    #[arg(long, required_if_eq("no_interactive", "true"))]
    /// Application Main activity
    main_activity: Option<String>,
    #[arg(long, short, action, required_if_eq("no_interactive", "true"))]
    /// Suppress logs
    quiet: bool,
    /// Project description
    #[arg(long, short, required_if_eq("no_interactive", "true"))]
    description: Option<String>,
}

pub struct Init {
    pub args: InitArgs,
}

pub struct ProjectPaths {
    pub root: PathBuf,
    pub app: PathBuf,
    pub package: PathBuf,
    pub res: PathBuf,
}

impl Init {
    pub fn new(args: &InitArgs) -> Init {
        Init { args: args.clone() }
    }
    fn interactive(&mut self) -> dialoguer::Result<()> {
        // Query user for the project name
        // Check if the name was provided as an argument
        let default_name = match &self.args.name {
            Some(n) => n.clone(),
            None => String::from(""),
        };

        // prompt the user, add the provided name as placeholder
        let name = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("Project name")
            .allow_empty(false)
            .show_default(self.args.name.is_some())
            .validate_with(|input: &String| {
                if input.trim().is_empty() {
                    Err("Value is required!")
                } else {
                    Ok(())
                }
            })
            .default(default_name)
            .interact_text()?;

        // check if the description was already fed through command line args
        // if it exists , set the value of default description to that of the
        // argument
        let default_description = match &self.args.description {
            Some(d) => d.clone(),
            None => String::from(""),
        };

        // prompt the user for the project description
        let descriprion = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("description")
            .allow_empty(true)
            .show_default(self.args.description.is_some())
            .default(default_description)
            .interact_text()?;

        // prompt the user, for package name
        let package = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("Package name")
            .validate_with(|input: &String| {
                if input.is_empty() {
                    Err("Value required")
                } else if let Ok(re) = Regex::new(r"^([a-z]+(\.)?)+$") {
                    if !re.is_match(input.as_str()) {
                        Err("Please provide a valid package Name. e.g. com.example.app")
                    } else {
                        Ok(())
                    }
                } else {
                    Ok(())
                }
            })
            .interact_text()?;
        self.args.package = Some(package);
        self.args.name = Some(name);
        self.args.description = Some(descriprion);

        // prompt user for version number
        let version_number = Input::<i32>::with_theme(&ColorfulTheme::default())
            .with_prompt("Version number")
            .validate_with(|input: &i32| {
                if input.is_negative() {
                    Err("Provide a positive number")
                } else {
                    Ok(())
                }
            })
            .default(1)
            .show_default(true)
            .allow_empty(false)
            .interact_text()?;

        self.args.version_number = Some(version_number);

        // prompt user for version none
        let version_name = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("Version name")
            .allow_empty(false)
            .default("0.1.0".to_string())
            .show_default(true)
            .validate_with(|input: &String| {
                if input.trim().is_empty() {
                    Err("Value cant be empty!")
                } else {
                    Ok(())
                }
            })
            .interact_text()?;

        self.args.version_name = Some(version_name);

        let main_activity = Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt("Main Activity")
            .default("MainActivity".to_string())
            .show_default(true)
            .validate_with(|input: &String| {
                if input.trim().is_empty() {
                    Err("Value can't be empty!")
                } else {
                    Ok(())
                }
            })
            .interact()?;
        self.args.main_activity = Some(main_activity);

        Ok(())
    }
    /// builds the project directory structure from the provided
    /// project root path. If force_use_cwd is set to true, this
    /// function tries to build the path structure from the root_path
    /// instead of building a subfolder with project name as the new root_path
    fn build_tree(&self, root_path: &Path, force_use_cwd: bool) -> Result<ProjectPaths, io::Error> {
        // Create project folder
        let mut path = root_path.to_path_buf();

        // canonicalize if relative in order to allow extracting of
        // the parent folder name
        if path.is_relative() {
            path = path.canonicalize().map_err(|err| {
                io::Error::new(err.kind(), format!("Unable to canonicalize path: {}", err))
            })?;
        }

        // Try to use the project name as the destination directory,
        // first check if use of current working dir is required. this is
        // set if the project name was not set and it was inferred from the
        // directory name.
        if !force_use_cwd {
            if let Some(name) = &self.args.name {
                path.push(name.clone());
            }
        }

        // check if directory exists, else create a new dir
        if path.exists() {
            // check if path is empty
            if !self.is_dir_empty(&path)? {
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    "The target directory exists and is not empty",
                ));
            }
        } else {
            fs::create_dir(&path)?;
        }

        // Create corresponding paths
        let app_path: PathBuf = path.join("app");
        if !app_path.exists() {
            fs::create_dir(&app_path)?;
        }

        // java folder with packages
        let package = &self
            .args
            .package
            .clone()
            .unwrap_or("com.example.app".to_string())
            .replace('.', "/");

        let mut java_path: PathBuf = app_path.join("java");
        java_path.push(package);

        fs::create_dir_all(&java_path)?;

        // res folder

        let res_path: PathBuf = app_path.join("res");

        let drawables = ["", "hdpi", "ldpi", "mdpi", "xhdpi", "xxhdpi", "xxxhdpi"];

        // create drawables
        for drawable_type in drawables {
            if drawable_type.is_empty() {
                fs::create_dir_all(res_path.join("drawables"))?;
            } else {
                fs::create_dir_all(res_path.join(format!("drawable-{}", drawable_type)))?;
            }
        }

        // layout
        fs::create_dir_all(res_path.join("layout"))?;
        // menu
        fs::create_dir_all(res_path.join("menu"))?;
        // values
        fs::create_dir_all(res_path.join("values"))?;
        // xml
        fs::create_dir_all(res_path.join("xml"))?;

        // assets
        fs::create_dir_all(app_path.join("assets"))?;

        Ok(ProjectPaths {
            res: res_path,
            root: path,
            package: java_path,
            app: app_path,
        })
    }
    fn is_dir_empty(&self, path: &Path) -> io::Result<bool> {
        if !path.exists() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "Path does not exist",
            ));
        }
        if !path.is_dir() {
            return Err(io::Error::new(
                io::ErrorKind::Other,
                "The path provided is not a directory. A file with the same name exists!",
            ));
        }

        match path.read_dir() {
            Ok(mut files) => Ok(files.next().is_none()),
            Err(e) => Err(e),
        }
    }
    pub fn template_files(&self, paths: &ProjectPaths) -> anyhow::Result<()> {
        let args = self.args.clone();

        {
            // Render an AndroidManifest.xml
            let mut path = paths.app.clone();
            let action = "AndroidManifest-Templating";
            path.push("AndroidManifest.xml");

            // Create manifest file
            let mut file = File::create(&path).context(format!(
                "Creating file for {} - Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;

            // Run template
            let manifest = AndroidManifest::new(
                args.package
                    .to_owned()
                    .unwrap_or("com.example.app".to_string())
                    .as_str(),
                args.version_number.to_owned().unwrap_or(1),
                args.version_name
                    .to_owned()
                    .unwrap_or("1.0.0".to_string())
                    .as_str(),
                args.main_activity
                    .to_owned()
                    .unwrap_or("MainActivity".to_string())
                    .as_str(),
            );
            // Render manifest and return rendered string
            let data = manifest.render_once().context(format!(
                "Rendering for {} Path: {}",
                action,
                path.to_str().unwrap_or("")
            ))?;

            // Write rendered string to file
            file.write_all(data.as_bytes()).context(format!(
                "Writing data for {} to {}",
                action,
                path.to_str().unwrap_or("")
            ))?;
        }

        // Main Activity.java
        {
            let mut path = paths.package.clone();
            let action = "MainActivity-Templating";
            path.push({
                match args.main_activity.to_owned() {
                    Some(class) => class + ".java",
                    None => "MainActivity.java".to_string(),
                }
            });
            // Create file
            let mut file = File::create(&path).context(format!(
                "Creating file for {} - Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;

            // initialize template
            let activity = Activity::new(
                args.package
                    .to_owned()
                    .unwrap_or("com.example.app".to_string())
                    .as_str(),
                args.main_activity
                    .to_owned()
                    .unwrap_or("MainActivity".to_string())
                    .as_str(),
                Some("activity_main".to_string()),
            );

            // generate template
            let data = activity.render_once().context(format!(
                "Rendering for {} Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;

            // write data to file
            file.write_all(data.as_bytes()).context(format!(
                "Writing data for {} - to File: {}",
                action,
                path.to_str().unwrap_or("")
            ))?;
        }
        {
            let path = paths.res.join("layout/activity_main.xml");
            let action = "XML-Activity-Templating";
            let mut file = File::create(&path).context(format!(
                "Creating file for {} - Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;
            let activity_main = ActivityXml::new();

            // render template
            let data = activity_main.render_once().context(format!(
                "Rendering for {} Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;

            // write rendered template to file
            file.write_all(data.as_bytes()).context(format!(
                "Writing data for {} - to File: {}",
                action,
                path.to_str().unwrap_or("")
            ))?;
        }
        {
            let mut path = paths.res.clone();
            path.push("values/strings.xml");
            let action = "Strings-Templating";

            let mut file = File::create(&path).context(format!(
                "Creating file for {} - Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;
            let strings_xml = StringsRes::new(
                args.name.to_owned().unwrap_or("App".to_string()).as_str(),
                args.main_activity
                    .to_owned()
                    .unwrap_or("App".to_string())
                    .as_str(),
            );
            // render template
            let data = strings_xml.render_once().context(format!(
                "Rendering for {} Path: {}",
                &action,
                path.to_str().unwrap_or("")
            ))?;

            // write rendered template to file
            file.write_all(data.as_bytes()).context(format!(
                "Writing data for {} - to File: {}",
                action,
                path.to_str().unwrap_or("")
            ))?;
        }
        {
            // Write the project .toml file
            let mut path = paths.root.clone();
            let args = self.args.clone();
            let toml = LabToml {
                project: Project {
                    name: args.name.unwrap_or("myapp".to_string()),
                    description: args.description.unwrap_or("".to_string()),
                    version_number: args.version_number.unwrap_or(1),
                    version: args.version_name.unwrap_or("0.1.0".to_string()),
                    package: args.package.unwrap_or("com.example".to_string()),
                },
                dependencies: None,
                resolvers: None,
                plugins: None,
            };
            // serialize to toml string
            let toml = toml::to_string(&toml).context("Serializing LabtToml to toml string")?;
            path.push("Labt.toml");

            // create file target to write toml file
            let mut file = File::create(&path).context(format!(
                "Creating Labt.toml file at {}",
                path.to_str().unwrap_or("[unknown]")
            ))?;

            // write the toml to file
            file.write_all(toml.as_bytes()).context(format!(
                "Writing LabtToml string to toml file at {}",
                path.to_str().unwrap_or("[unknown]")
            ))?;
        }
        Ok(())
    }
}

impl Submodule for Init {
    /*
        ============Entry point for this module =================
    */

    /// Executed by this module loader, it receives the commandline
    /// arguments for this subcommand stored in self.args
    /// This is the entry point for Init subcommand
    fn run(&mut self) -> anyhow::Result<()> {
        let mut force_use_cwd = false;

        if self.args.path.is_none() {
            let cwd = std::env::current_dir()?;
            // infer the project name from directory name
            if self.args.name.is_none() {
                self.args.name = cwd
                    .file_name()
                    .map(|n| n.to_str().unwrap_or("").to_string());
                force_use_cwd = true;
            }
            self.args.path = Some(cwd);
        }

        if force_use_cwd && self.args.path.is_some() {
            // check if directory is not empty
            if !self.is_dir_empty(self.args.path.clone().unwrap().as_path())? {
                bail!("The target directory is not empty");
            }
        }
        // if interactive is disabled make all other flags compulsury
        if !self.args.no_interactive {
            if let Err(err) = self.interactive() {
                bail!(err);
            }
        }
        if let Some(path) = &self.args.path {
            let project_paths = self.build_tree(path, force_use_cwd)?;
            self.template_files(&project_paths)?;
        } else {
            return Err(anyhow::anyhow!(
                "Could not identify a project path from the arguments nor the working directory"
            ));
        }
        Ok(())
    }
}