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
//! Files/directories that are created when initializing a book directory
//!
//! I wanted to do it automatically, but not sure how to do it.

use std::path::Path;

pub mod files {
    //! Init files in bytes

    pub static BOOK: &[u8] = include_bytes!("../../init/book.ron");

    pub static EDITOR_CONFIG: &[u8] = include_bytes!("../../init/.editorconfig");
    pub static GIT_IGNORE: &[u8] = include_bytes!("../../init/.gitignore");

    pub mod site {}

    pub mod src {
        pub static TOC: &[u8] = include_bytes!("../../init/src/toc.ron");
        pub static INDEX: &[u8] = include_bytes!("../../init/src/index.adoc");
        pub static ARTICLE: &[u8] = include_bytes!("../../init/src/article.adoc");

        pub mod static_ {
            pub mod img {}
        }

        pub mod theme {
            pub static FAVICON: &[u8] = include_bytes!("../../init/src/theme/favicon.svg");
            pub mod hbs {
                pub static ARTICLE: &[u8] = include_bytes!("../../init/src/theme/hbs/article.hbs");

                pub mod partials {
                    pub static SIDEBAR: &[u8] =
                        include_bytes!("../../init/src/theme/hbs/partials/sidebar.hbs");
                    pub static SIDEBAR_ITEM: &[u8] =
                        include_bytes!("../../init/src/theme/hbs/partials/sidebar_item.hbs");
                }
            }
            pub mod css {
                pub static TERM: &[u8] = include_bytes!("../../init/src/theme/css/term.css");
                pub mod partials {
                    pub static TERM_ADOC: &[u8] =
                        include_bytes!("../../init/src/theme/css/partials/term_adoc.css");
                    pub static PRISM_OKIDIA: &[u8] =
                        include_bytes!("../../init/src/theme/css/partials/prism_okidia.css");
                }
            }
            pub mod js {
                pub static PRISM: &[u8] = include_bytes!("../../init/src/theme/js/prism.js");
            }
        }
    }
}

/// List of init files
pub static LIST: &[(&str, &[u8]); 24] = {
    use files::src::{
        self,
        theme::{self, css, hbs, js},
    };

    &[
        // 3
        (".gitignore", files::GIT_IGNORE),
        (".editorconfig", files::EDITOR_CONFIG),
        ("book.ron", files::BOOK),
        // 1
        ("site", &[]),
        // 4
        ("src", &[]),
        ("src/toc.ron", src::TOC),
        ("src/index.adoc", src::INDEX),
        ("src/article.adoc", src::ARTICLE),
        // 2
        ("src/static", &[]),
        ("src/static/img", &[]),
        // 2
        ("src/theme", &[]),
        ("src/theme/favicon.svg", theme::FAVICON),
        // 5
        ("src/theme/hbs", &[]),
        ("src/theme/hbs/article.hbs", hbs::ARTICLE),
        ("src/theme/hbs/partials", &[]),
        ("src/theme/hbs/partials/sidebar.hbs", hbs::partials::SIDEBAR),
        (
            "src/theme/hbs/partials/sidebar_item.hbs",
            hbs::partials::SIDEBAR_ITEM,
        ),
        // 5
        ("src/theme/css", &[]),
        ("src/theme/css/term.css", css::TERM),
        ("src/theme/css/partials", &[]),
        ("src/theme/css/partials/term_adoc.css", css::partials::TERM_ADOC),
        (
            "src/theme/css/partials/prism_okidia.css",
            css::partials::PRISM_OKIDIA,
        ),
        // 2
        ("src/theme/js", &[]),
        ("src/theme/js/prism.js", js::PRISM),
    ]
};

/// Generates init file structure
pub fn gen_init_files(base_dir: &Path) -> std::io::Result<()> {
    use std::{fs, io};

    // helpers
    fn gen_dir(path: &Path) -> io::Result<bool> {
        if !path.exists() {
            fs::create_dir(path)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    fn gen_file(path: &Path, bytes: impl AsRef<[u8]>) -> io::Result<bool> {
        if !path.exists() {
            fs::write(path, bytes)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    for (rel_path_src, bytes) in LIST.iter() {
        let path = base_dir.join(rel_path_src);
        log::trace!("{}", path.display());
        if bytes.is_empty() {
            gen_dir(&path)?;
        } else {
            gen_file(&path, bytes)?;
        }
    }

    Ok(())
}