-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrate.cs
121 lines (97 loc) · 4.34 KB
/
Integrate.cs
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
using Mono.Unix;
using Utilities;
using Pastel;
using Utilities;
public class Integrate
{
public static void InstallApp(string pathToAppImage, string version = "n")
{
Console.WriteLine("Reading the Path...".Pastel("#1f7d34"));
var userHome = Environment.GetEnvironmentVariable("HOME");
var folderInPath = Path.GetDirectoryName(pathToAppImage);
Console.WriteLine("Unpacking the package...".Pastel("#1f7d34"));
ProgramEnvironment.CheckDirectories();
Environment.CurrentDirectory = $"{userHome}/.local/share/apps/cache";
try
{
CoreCommands.ExecShellCommand(pathToAppImage, "--appimage-extract");
}
catch (System.Exception)
{
Console.WriteLine("File execution error, try installing libfuse, or update libappimage");
}
var squashfsRoot = $"{Environment.CurrentDirectory}/squashfs-root";
Console.WriteLine("Search for the required files...".Pastel("#1f7d34"));
var desktopfiles = Directory.GetFiles(squashfsRoot, "*.desktop");
var pngfiles = Directory.GetFiles(squashfsRoot, "*.png");
var Content = File.ReadAllText(desktopfiles[0]).Split('\n');
Console.WriteLine("Package name generation...".Pastel("#1f7d34"));
var nameApp = Content[FindIndexOfWord(Content, "Name")]
.Split('=')[1]
.Replace(" ", String.Empty).ToLower();
var copyPngTo =
$"{userHome}/.local/share/apps/app_icons/{nameApp}.png";
var copyAppImageTo =
$"{userHome}/.local/share/apps/app_images/{nameApp}.AppImage";
var newDesktopFile =
$"{userHome}/.local/share/applications/{nameApp}.desktop";
var ufi = new UnixFileInfo(newDesktopFile);
Console.WriteLine("Checking directories...".Pastel("#1f7d34"));
Console.WriteLine("Copying files...".Pastel("#1f7d34"));
try
{
File.Copy(pngfiles[0], copyPngTo, true);
File.Copy(pathToAppImage, copyAppImageTo, true);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
Environment.Exit(1);
}
Console.WriteLine("Creating a \".desktop\" file...".Pastel("#1f7d34"));
Content[FindIndexOfWord(Content, "Exec")] = $"Exec={copyAppImageTo}";
Content[FindIndexOfWord(Content, "Icon")] = $"Icon={copyPngTo}";
File.WriteAllLines(desktopfiles[0], Content);
File.Copy(desktopfiles[0], newDesktopFile, true);
Console.WriteLine("Setting access parameters...".Pastel("#1f7d34"));
ufi.FileAccessPermissions |= FileAccessPermissions.UserExecute;
Directory.Delete(squashfsRoot, true);
try
{
DataBaseUtils.CreateTableOrNo(DataBaseUtils.Connection);
}
catch (Exception)
{}
DataBaseUtils.InsertApp(DataBaseUtils.Connection, nameApp, newDesktopFile, copyAppImageTo, version);
Console.WriteLine($"The application is installed, to remove it later - specify the phrase \"{nameApp}\" instead of the file path, and use \"remove\" instead of \"install\".".Pastel("#edea15"));
}
public static void RemoveApp(string appName, bool NoOutput = false)
{
if(NoOutput != true)
{
Console.WriteLine("Searching for an app...".Pastel("#1f7d34"));
}
if (DataBaseUtils.checkApp(DataBaseUtils.Connection, appName))
{
var App = DataBaseUtils.ReadAppInfo(DataBaseUtils.Connection, appName);
if(NoOutput != true)
{
Console.WriteLine("Removing ALL traces of the application...".Pastel("#1f7d34"));
}
File.Delete(App.DesktopFile);
File.Delete(App.AppImageFile);
DataBaseUtils.DeleteApp(DataBaseUtils.Connection, appName);
}
else
{
Console.WriteLine("The application is not installed!".Pastel("#edea15"));
}
}
public static int FindIndexOfWord(string[] array, string word) =>
array
.Select((str, i) => (str.Split('='), i))
.Where(x => x.Item1.Any(w => w.Equals(word, StringComparison.OrdinalIgnoreCase)))
.Select(x => x.Item2)
.Cast<int?>()
.FirstOrDefault() ?? -1;
}