install_framework_core/interface.rs
1// Copyright 2021 Yuri6037
2
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21use std::path::Path;
22use std::collections::HashMap;
23
24use crate::command::CommandQueue;
25use crate::command::InitCommand;
26use crate::command::InstallCommand;
27use crate::builder::InstallerBuilder;
28
29#[derive(Clone)]
30pub struct Component
31{
32 /// name of component
33 pub name: String,
34
35 /// version of component
36 pub version: Option<String>,
37
38 /// license text of component
39 pub license: Option<String>,
40
41 /// readme text of component
42 pub readme: Option<String>
43}
44
45pub trait Installer : Send
46{
47 /// Executes before any other function in order to prepare the environment for subsequent component installs
48 ///
49 /// # Arguments
50 ///
51 /// * `commands` - the command queue to push commands into
52 fn init(&mut self, commands: &mut CommandQueue<InitCommand>);
53
54 /// Returns all components this installer is able to install
55 ///
56 /// # Arguments
57 ///
58 /// * `cache` - access to the cache path
59 fn get_components(&mut self, cache: &Path) -> Vec<Component>;
60
61 /// Installs a given component
62 ///
63 /// # Arguments
64 ///
65 /// * `component` - component name
66 /// * `commands` - the command queue to push commands into
67 fn install_component(&mut self, component: &str, commands: &mut CommandQueue<InstallCommand>);
68}
69
70#[derive(Clone, Copy)]
71pub enum InstallMethod
72{
73 /// Identifies the current install mode as local to the current connected user only
74 UserInstall,
75
76 /// Identifies the current install mode as system wide (for all users)
77 SystemInstall
78}
79
80/// Represents a custom post install step
81pub trait PostInstall : Send
82{
83 /// Called after all installations have been performed (only if a PostInstall has been defined in InstallerBuilder)
84 ///
85 /// # Arguments
86 ///
87 /// * `cache` - access to the cache path
88 /// * `content_dir` - access to the content directory of the installed application
89 /// * `bin_dir` - access to the binary directory (directory which is present in PATH when one or more AddToPath rules are present)
90 fn post_install(&mut self, cache: &Path, content_dir: &Path, bin_dir: &Path);
91}
92
93/// Represents a custom post uninstall step
94pub trait PostUninstall : Send
95{
96 /// Called after all uninstallations have been performed (only if a PostInstall has been defined in InstallerBuilder)
97 ///
98 /// # Arguments
99 ///
100 /// * `cache` - access to the cache path
101 /// * `content_dir` - access to the content directory of the installed application
102 /// * `bin_dir` - access to the binary directory (directory which is present in PATH when one or more AddToPath rules are present)
103 fn post_uninstall(&mut self, cache: &Path, content_dir: &Path, bin_dir: &Path);
104}
105
106/// Represents an interface
107pub trait Interface
108{
109 type ErrorType;
110
111 /// Opens the welcome page for this interface
112 ///
113 /// # Arguments
114 ///
115 /// * `installer_name` - the name of this installer
116 /// * `installer_version` - the version of this installer
117 /// * `installer_author` - the author or company name of this installer
118 fn welcome(&mut self, installer_name: &'static str, installer_version: &'static str, installer_author: &'static str) -> Result<(), Self::ErrorType>;
119
120 /// Returns the install method the user chose (only called if the installer supports both modes to decide which mode should be used to continue)
121 ///
122 /// # Returns
123 ///
124 /// an InstallMethod
125 fn get_install_method(&mut self) -> Result<InstallMethod, Self::ErrorType>;
126
127 /// Returns wether the user chose the uninstall option or the install option
128 fn should_uninstall(&self) -> Result<bool, Self::ErrorType>;
129
130 /// Runs the install procedure for this interface
131 ///
132 /// # Arguments
133 ///
134 /// * `install` - the installer trait
135 /// * `install_dir` - the computed installation directory
136 /// * `method` - the installation method
137 /// * `resources` - the resources map built by InstallerBuilder
138 fn run_install(&mut self, install: &mut dyn Installer, install_dir: &Path, method: InstallMethod, resources: &HashMap<&'static str, &'static [u8]>) -> Result<(), Self::ErrorType>;
139
140 /// Runs the post-install procedure for this interface
141 ///
142 /// # Arguments
143 ///
144 /// * `post_install` - the custom PostInstall step
145 /// * `install_dir` - the computed installation directory
146 fn run_post_install(&mut self, post_install: &mut dyn PostInstall, install_dir: &Path) -> Result<(), Self::ErrorType>;
147
148 /// Runs the uninstall procedure for this interface
149 ///
150 /// # Arguments
151 ///
152 /// * `install` - the installer trait
153 /// * `install_dir` - the computed installation directory
154 /// * `method` - the installation method
155 /// * `resources` - the resources map built by InstallerBuilder
156 fn run_uninstall(&mut self, install: &mut dyn Installer, install_dir: &Path, method: InstallMethod, resources: &HashMap<&'static str, &'static [u8]>) -> Result<(), Self::ErrorType>;
157
158 /// Runs the post-uninstall procedure for this interface
159 ///
160 /// # Arguments
161 ///
162 /// * `post_uninstall` - the custom PostUninstall step
163 /// * `install_dir` - the computed installation directory
164 fn run_post_uninstall(&mut self, post_uninstall: &mut dyn PostUninstall, install_dir: &Path) -> Result<(), Self::ErrorType>;
165
166 /// Called when an error has occured
167 ///
168 /// # Arguments
169 ///
170 /// * `err` - the error object
171 ///
172 /// # Returns
173 ///
174 /// expected program exit code for the given error object
175 fn error(&mut self, err: Self::ErrorType) -> i32;
176
177 /// Called when no error has occured and the installer is finished
178 ///
179 /// # Returns
180 ///
181 /// expected program exit code
182 fn finish(&mut self) -> i32;
183}
184
185/// Allows an interface to be automatically constructed by the unified run system
186pub trait ConstructibleInterface<'a>
187{
188 /// Runs the installer for the given InstallerBuilder
189 ///
190 /// # Arguments
191 ///
192 /// * `builder` - the builder to run
193 ///
194 /// # Returns
195 ///
196 /// expected program exit code
197 fn run(builder: InstallerBuilder<'a>) -> i32;
198}