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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{
    inputs::{InputFile, InputsDirectory},
    root::Gitignore,
    source::{MainFile, SourceDirectory},
};

use leo_errors::{PackageError, Result};

use crate::build::BuildDirectory;
use serde::Deserialize;
use std::path::Path;

#[derive(Deserialize)]
pub struct Package {
    pub name: String,
    pub version: String,
    pub description: Option<String>,
    pub license: Option<String>,
}

impl Package {
    pub fn new(package_name: &str) -> Result<Self> {
        // Check that the package name is valid.
        if !Self::is_package_name_valid(package_name) {
            return Err(PackageError::invalid_package_name(package_name).into());
        }

        Ok(Self { name: package_name.to_owned(), version: "0.1.0".to_owned(), description: None, license: None })
    }

    /// Returns `true` if the package name is valid.
    ///
    /// Package names can only contain ASCII alphanumeric characters and underscores.
    pub fn is_package_name_valid(package_name: &str) -> bool {
        // Check that the package name is nonempty.
        if package_name.is_empty() {
            tracing::error!("Project names must be nonempty");
            return false;
        }

        let first = package_name.chars().next().unwrap();

        // Check that the first character is not an underscore.
        if first == '_' {
            tracing::error!("Project names cannot begin with an underscore");
            return false;
        }

        // Check that the first character is not a number.
        if first.is_numeric() {
            tracing::error!("Project names cannot begin with a number");
            return false;
        }

        // Iterate and check that the package name is valid.
        for current in package_name.chars() {
            // Check that the package name contains only ASCII alphanumeric or underscores.
            if !current.is_ascii_alphanumeric() && current != '_' {
                tracing::error!("Project names must can only contain ASCII alphanumeric characters and underscores.");
                return false;
            }
        }

        true
    }

    /// Returns `true` if a package is can be initialized at a given path.
    pub fn can_initialize(package_name: &str, path: &Path) -> bool {
        // Check that the package name is valid.
        if !Self::is_package_name_valid(package_name) {
            return false;
        }

        let mut result = true;
        let mut existing_files = vec![];

        // Check if the input file already exists.
        let input_file = InputFile::new(package_name);
        if input_file.exists_at(path) {
            existing_files.push(input_file.filename());
            result = false;
        }

        // Check if the main file already exists.
        if MainFile::exists_at(path) {
            existing_files.push(MainFile::filename());
            result = false;
        }

        if !existing_files.is_empty() {
            tracing::error!("File(s) {:?} already exist", existing_files);
        }

        result
    }

    /// Returns `true` if a package is initialized at the given path
    pub fn is_initialized(package_name: &str, path: &Path) -> bool {
        // Check that the package name is valid.
        if !Self::is_package_name_valid(package_name) {
            return false;
        }

        // Check if the input file exists.
        let input_file = InputFile::new(package_name);
        if !input_file.exists_at(path) {
            return false;
        }

        // Check if the main file exists.
        if !MainFile::exists_at(path) {
            return false;
        }

        true
    }

    /// Creates a Leo package at the given path
    pub fn initialize(package_name: &str, path: &Path) -> Result<()> {
        // Verify that the .gitignore file does not exist.
        if !Gitignore::exists_at(path) {
            // Create the .gitignore file.
            Gitignore::new().write_to(path)?;
        }

        // Create the source directory.
        SourceDirectory::create(path)?;

        // Create the inputs directory.
        InputsDirectory::create(path)?;

        // Create the Leo build/ directory
        BuildDirectory::create(path)?;

        // Create the input file in the inputs directory.
        InputFile::new(package_name).write_to(path)?;

        // Create the main file in the source directory.
        MainFile::new(package_name).write_to(path)?;

        // Next, verify that a valid Leo package has been initialized in this directory
        if !Self::is_initialized(package_name, path) {
            return Err(PackageError::failed_to_initialize_package(package_name, path.as_os_str()).into());
        }

        Ok(())
    }
    //
    // /// Removes the package at the given path
    // pub fn remove_imported_package(package_name: &str, path: &Path) -> Result<()> {
    //     ImportsDirectory::remove_import(path, package_name)
    // }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_package_name_valid() {
        assert!(Package::is_package_name_valid("foo"));
        assert!(Package::is_package_name_valid("foo_bar"));
        assert!(Package::is_package_name_valid("foo1"));
        assert!(Package::is_package_name_valid("foo_bar___baz_"));

        assert!(!Package::is_package_name_valid("foo-bar"));
        assert!(!Package::is_package_name_valid("foo-bar-baz"));
        assert!(!Package::is_package_name_valid("foo-1"));
        assert!(!Package::is_package_name_valid(""));
        assert!(!Package::is_package_name_valid("-"));
        assert!(!Package::is_package_name_valid("-foo"));
        assert!(!Package::is_package_name_valid("-foo-"));
        assert!(!Package::is_package_name_valid("_foo"));
        assert!(!Package::is_package_name_valid("foo--bar"));
        assert!(!Package::is_package_name_valid("foo---bar"));
        assert!(!Package::is_package_name_valid("foo--bar--baz"));
        assert!(!Package::is_package_name_valid("foo---bar---baz"));
        assert!(!Package::is_package_name_valid("foo*bar"));
        assert!(!Package::is_package_name_valid("foo,bar"));
        assert!(!Package::is_package_name_valid("1-foo"));
    }
}