ShareX/ShareX.Setup/Program.cs

382 lines
15 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2022-01-12 05:32:17 +13:00
Copyright (c) 2007-2022 ShareX Team
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2018-03-03 10:06:40 +13:00
using ShareX.HelpersLib;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
2014-12-11 09:25:20 +13:00
namespace ShareX.Setup
{
internal class Program
{
2016-08-27 12:28:05 +12:00
[Flags]
private enum SetupJobs
{
2016-08-27 12:28:05 +12:00
None = 0,
CreateSetup = 1,
CreatePortable = 1 << 1,
CreateSteamFolder = 1 << 2,
2022-01-11 12:52:15 +13:00
OpenOutputDirectory = 1 << 3,
UploadOutputFile = 1 << 4,
CreateWindowsStoreFolder = 1 << 5,
CreateWindowsStoreDebugFolder = 1 << 6,
CompileAppx = 1 << 7,
DownloadFFmpeg = 1 << 8,
CreateChecksumFile = 1 << 9,
2016-08-27 12:28:05 +12:00
2021-11-10 21:57:10 +13:00
Stable = CreateSetup | CreatePortable | CreateChecksumFile | OpenOutputDirectory,
2016-08-27 12:28:05 +12:00
Setup = CreateSetup | OpenOutputDirectory,
Portable = CreatePortable | OpenOutputDirectory,
Steam = CreateSteamFolder | OpenOutputDirectory,
2017-04-27 12:00:21 +12:00
WindowsStore = CreateWindowsStoreFolder,
WindowsStoreDebug = CreateWindowsStoreDebugFolder,
Beta = CreateSetup | UploadOutputFile,
2021-11-10 21:57:10 +13:00
AppVeyorRelease = CreateSetup | CreatePortable | CreateChecksumFile,
2017-05-30 01:35:31 +12:00
AppVeyorSteam = CreateSteamFolder,
AppVeyorWindowsStore = CreateWindowsStoreFolder | CompileAppx,
AppVeyorSteamRelease = AppVeyorSteam | DownloadFFmpeg,
AppVeyorWindowsStoreRelease = AppVeyorWindowsStore | DownloadFFmpeg
}
2018-03-17 08:58:25 +13:00
private static SetupJobs Job = SetupJobs.Stable;
2016-08-27 12:28:05 +12:00
private static bool AppVeyor = false;
2015-09-28 01:46:29 +13:00
2017-11-03 04:21:23 +13:00
private static string ParentDir => AppVeyor ? "." : @"..\..\..\";
2016-08-26 13:51:37 +12:00
private static string BinDir => Path.Combine(ParentDir, "ShareX", "bin");
private static string ReleaseDir => Path.Combine(BinDir, "Release");
2021-09-06 06:21:41 +12:00
private static string ReleaseExecutablePath => Path.Combine(ReleaseDir, "ShareX.exe");
2016-08-26 13:51:37 +12:00
private static string DebugDir => Path.Combine(BinDir, "Debug");
private static string DebugExecutablePath => Path.Combine(DebugDir, "ShareX.exe");
2016-08-26 13:51:37 +12:00
private static string SteamDir => Path.Combine(BinDir, "Steam");
2017-04-21 11:17:34 +12:00
private static string WindowsStoreDir => Path.Combine(BinDir, "WindowsStore");
2017-04-22 05:08:29 +12:00
private static string WindowsStoreDebugDir => Path.Combine(BinDir, "WindowsStoreDebug");
2016-08-27 12:02:43 +12:00
private static string OutputDir => Path.Combine(ParentDir, "Output");
2016-08-27 12:02:43 +12:00
private static string PortableOutputDir => Path.Combine(OutputDir, "ShareX-portable");
private static string SteamOutputDir => Path.Combine(OutputDir, "ShareX-Steam");
2017-04-21 11:17:34 +12:00
private static string WindowsStoreOutputDir => Path.Combine(OutputDir, "ShareX-WindowsStore");
2017-04-23 05:38:19 +12:00
private static string SetupDir => Path.Combine(ParentDir, "ShareX.Setup");
private static string InnoSetupDir => Path.Combine(SetupDir, "InnoSetup");
private static string WindowsStorePackageFilesDir => Path.Combine(SetupDir, "WindowsStore");
2016-08-27 12:02:43 +12:00
2016-08-27 10:55:30 +12:00
private static string SteamLauncherDir => Path.Combine(ParentDir, @"ShareX.Steam\bin\Release");
2016-08-26 13:51:37 +12:00
private static string SteamUpdatesDir => Path.Combine(SteamOutputDir, "Updates");
private static string NativeMessagingHostDir => Path.Combine(ParentDir, @"ShareX.NativeMessagingHost\bin\Release");
private static string RecorderDevicesSetupPath => Path.Combine(OutputDir, "Recorder-devices-setup.exe");
2017-05-18 09:32:38 +12:00
private static string WindowsStoreAppxPath => Path.Combine(OutputDir, "ShareX.appx");
2016-08-27 12:02:43 +12:00
2019-11-19 11:06:16 +13:00
public static string InnoSetupCompilerPath = @"C:\Program Files (x86)\Inno Setup 6\ISCC.exe";
public static string FFmpeg32bit => Path.Combine(OutputDir, "ffmpeg.exe");
public static string FFmpeg64bit => Path.Combine(OutputDir, "ffmpeg-x64.exe");
2020-03-10 21:01:51 +13:00
public static string MakeAppxPath = @"C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\makeappx.exe";
private static void Main(string[] args)
{
2016-08-27 12:28:05 +12:00
Console.WriteLine("ShareX setup started.");
2016-08-26 12:01:05 +12:00
if (SetupHelpers.CheckArguments(args, "-AppVeyorRelease"))
{
AppVeyor = true;
Job = SetupJobs.AppVeyorRelease;
}
else if (SetupHelpers.CheckArguments(args, "-AppVeyorSteam"))
2016-08-27 12:28:05 +12:00
{
AppVeyor = true;
Job = SetupJobs.AppVeyorSteam;
2016-08-27 12:28:05 +12:00
}
else if (SetupHelpers.CheckArguments(args, "-AppVeyorWindowsStore"))
2017-05-30 01:35:31 +12:00
{
AppVeyor = true;
Job = SetupJobs.AppVeyorWindowsStore;
}
else if (SetupHelpers.CheckArguments(args, "-AppVeyorSteamRelease"))
{
AppVeyor = true;
Job = SetupJobs.AppVeyorSteamRelease;
}
else if (SetupHelpers.CheckArguments(args, "-AppVeyorWindowsStoreRelease"))
{
AppVeyor = true;
Job = SetupJobs.AppVeyorWindowsStoreRelease;
}
2016-08-27 12:28:05 +12:00
Console.WriteLine("Setup job: " + Job);
2016-08-27 12:32:02 +12:00
if (Directory.Exists(OutputDir))
{
Console.WriteLine("Cleaning output directory: " + OutputDir);
Directory.Delete(OutputDir, true);
}
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.CreateSetup))
{
CompileSetup();
}
if (Job.HasFlag(SetupJobs.CreatePortable))
2016-08-26 12:01:05 +12:00
{
2017-04-22 05:08:29 +12:00
CreateFolder(ReleaseDir, PortableOutputDir, SetupJobs.CreatePortable);
2016-08-26 12:01:05 +12:00
}
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.CreateSteamFolder))
{
CreateSteamFolder();
if (Job.HasFlag(SetupJobs.DownloadFFmpeg))
{
CopyFFmpeg(SteamUpdatesDir, true, true);
}
2016-08-27 12:28:05 +12:00
}
2017-04-21 11:17:34 +12:00
if (Job.HasFlag(SetupJobs.CreateWindowsStoreFolder))
{
2017-04-22 05:08:29 +12:00
CreateFolder(WindowsStoreDir, WindowsStoreOutputDir, SetupJobs.CreateWindowsStoreFolder);
if (Job.HasFlag(SetupJobs.DownloadFFmpeg))
{
CopyFFmpeg(WindowsStoreOutputDir, false, true);
}
2017-04-22 05:08:29 +12:00
}
if (Job.HasFlag(SetupJobs.CreateWindowsStoreDebugFolder))
{
CreateFolder(WindowsStoreDebugDir, WindowsStoreOutputDir, SetupJobs.CreateWindowsStoreDebugFolder);
2017-04-21 11:17:34 +12:00
}
if (Job.HasFlag(SetupJobs.CompileAppx))
{
2018-12-07 07:51:41 +13:00
using (Process process = new Process())
{
2018-12-07 07:51:41 +13:00
ProcessStartInfo psi = new ProcessStartInfo()
{
2018-03-23 19:27:21 +13:00
FileName = MakeAppxPath,
Arguments = $"pack /d \"{WindowsStoreOutputDir}\" /p \"{WindowsStoreAppxPath}\" /l /o",
UseShellExecute = false,
RedirectStandardOutput = true
2018-12-07 07:51:41 +13:00
};
process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
process.StartInfo = psi;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
Directory.Delete(WindowsStoreOutputDir, true);
}
2021-11-10 21:57:10 +13:00
if (Job.HasFlag(SetupJobs.CreateChecksumFile))
{
Console.WriteLine("Creating checksum files.");
foreach (string file in Directory.GetFiles(OutputDir))
{
Helpers.CreateChecksumFile(file);
}
}
if (AppVeyor)
{
Helpers.CopyAll(OutputDir, ParentDir);
}
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.OpenOutputDirectory))
{
OpenOutputDirectory();
}
2015-09-06 21:32:34 +12:00
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.UploadOutputFile))
{
2016-08-27 12:28:05 +12:00
UploadLatestFile();
}
2016-08-27 12:28:05 +12:00
Console.WriteLine("ShareX setup successfully completed.");
2016-08-26 12:01:05 +12:00
}
2014-11-22 02:34:50 +13:00
private static void CompileSetup()
{
CompileISSFile("Recorder-devices-setup.iss");
CompileISSFile("ShareX-setup.iss");
}
2021-12-12 10:21:19 +13:00
private static void CompileISSFile(string fileName)
2016-08-26 15:41:34 +12:00
{
if (File.Exists(InnoSetupCompilerPath))
{
2021-12-12 10:21:19 +13:00
Console.WriteLine("Compiling setup file: " + fileName);
2016-08-26 15:41:34 +12:00
2018-12-07 07:51:41 +13:00
using (Process process = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = InnoSetupCompilerPath,
WorkingDirectory = Path.GetFullPath(InnoSetupDir),
2021-12-12 10:21:19 +13:00
Arguments = $"\"{fileName}\"",
2018-12-07 07:51:41 +13:00
UseShellExecute = false
};
process.StartInfo = psi;
process.Start();
process.WaitForExit();
}
2016-08-26 15:41:34 +12:00
Console.WriteLine("Setup file is created.");
}
else
{
Console.WriteLine("InnoSetup compiler is missing: " + InnoSetupCompilerPath);
}
2016-08-26 15:41:34 +12:00
}
2015-09-06 21:32:34 +12:00
private static void CreateSteamFolder()
{
Console.WriteLine("Creating Steam folder:" + SteamOutputDir);
2016-08-26 13:51:37 +12:00
if (Directory.Exists(SteamOutputDir))
2015-09-06 21:32:34 +12:00
{
2016-08-26 13:51:37 +12:00
Directory.Delete(SteamOutputDir, true);
2015-09-06 21:32:34 +12:00
}
2016-08-26 13:51:37 +12:00
Directory.CreateDirectory(SteamOutputDir);
2015-09-06 21:32:34 +12:00
SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "ShareX_Launcher.exe"), SteamOutputDir);
SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "steam_appid.txt"), SteamOutputDir);
SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "installscript.vdf"), SteamOutputDir);
SetupHelpers.CopyFiles(SteamLauncherDir, "*.dll", SteamOutputDir);
2015-09-06 21:32:34 +12:00
2017-04-22 05:08:29 +12:00
CreateFolder(SteamDir, SteamUpdatesDir, SetupJobs.CreateSteamFolder);
2015-09-06 21:32:34 +12:00
}
2017-04-22 05:08:29 +12:00
private static void CreateFolder(string source, string destination, SetupJobs job)
2017-04-21 11:17:34 +12:00
{
2017-04-22 05:08:29 +12:00
Console.WriteLine("Creating folder: " + destination);
2017-04-21 11:17:34 +12:00
2017-04-22 05:08:29 +12:00
if (Directory.Exists(destination))
2017-04-21 11:17:34 +12:00
{
2017-04-22 05:08:29 +12:00
Directory.Delete(destination, true);
2017-04-21 11:17:34 +12:00
}
2017-04-22 05:08:29 +12:00
Directory.CreateDirectory(destination);
2017-04-21 11:17:34 +12:00
SetupHelpers.CopyFile(Path.Combine(source, "ShareX.exe"), destination);
SetupHelpers.CopyFile(Path.Combine(source, "ShareX.exe.config"), destination);
2018-03-03 10:31:06 +13:00
SetupHelpers.CopyFiles(source, "*.dll", destination);
2017-04-22 05:08:29 +12:00
if (job == SetupJobs.CreateWindowsStoreDebugFolder)
2014-11-14 12:28:07 +13:00
{
SetupHelpers.CopyFiles(source, "*.pdb", destination);
2014-11-14 12:28:07 +13:00
}
SetupHelpers.CopyFiles(Path.Combine(ParentDir, "Licenses"), "*.txt", Path.Combine(destination, "Licenses"));
2016-08-28 10:08:18 +12:00
if (job != SetupJobs.CreateWindowsStoreFolder && job != SetupJobs.CreateWindowsStoreDebugFolder)
{
if (!File.Exists(RecorderDevicesSetupPath))
{
CompileISSFile("Recorder-devices-setup.iss");
}
SetupHelpers.CopyFile(RecorderDevicesSetupPath, destination);
2016-08-28 10:08:18 +12:00
SetupHelpers.CopyFile(Path.Combine(NativeMessagingHostDir, "ShareX_NativeMessagingHost.exe"), destination);
}
2020-10-14 04:41:45 +13:00
string[] languages = new string[] { "de", "es", "es-MX", "fa-IR", "fr", "hu", "id-ID", "it-IT", "ja-JP", "ko-KR", "nl-NL", "pt-BR", "pt-PT", "ru", "tr", "uk", "vi-VN", "zh-CN", "zh-TW" };
2015-01-05 05:53:04 +13:00
foreach (string language in languages)
{
SetupHelpers.CopyFiles(Path.Combine(source, language), "*.resources.dll", Path.Combine(destination, "Languages", language));
2015-01-05 05:53:04 +13:00
}
2018-03-17 08:58:25 +13:00
Helpers.CopyAll(Path.Combine(ParentDir, @"ShareX.ScreenCaptureLib\Stickers"), Path.Combine(destination, "Stickers"));
2022-01-11 12:52:15 +13:00
if (job == SetupJobs.CreateWindowsStoreFolder || job == SetupJobs.CreateWindowsStoreDebugFolder)
2017-04-21 11:17:34 +12:00
{
2017-04-23 05:38:19 +12:00
Helpers.CopyAll(WindowsStorePackageFilesDir, destination);
}
2017-04-22 05:08:29 +12:00
else if (job == SetupJobs.CreatePortable)
2015-09-06 21:32:34 +12:00
{
2017-04-22 05:08:29 +12:00
Helpers.CreateEmptyFile(Path.Combine(destination, "Portable"));
2021-09-06 06:21:41 +12:00
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(ReleaseExecutablePath);
2021-12-12 10:21:19 +13:00
string zipFileName = string.Format("ShareX-{0}.{1}.{2}-portable.zip", versionInfo.ProductMajorPart, versionInfo.ProductMinorPart, versionInfo.ProductBuildPart);
string zipPath = Path.Combine(OutputDir, zipFileName);
2021-09-06 06:21:41 +12:00
//string zipPath = Path.Combine(OutputDir, "ShareX-portable.zip");
2018-03-03 10:06:40 +13:00
ZipManager.Compress(Path.GetFullPath(destination), Path.GetFullPath(zipPath));
2014-01-13 05:05:31 +13:00
2017-05-18 09:32:38 +12:00
Directory.Delete(destination, true);
2014-01-16 08:12:59 +13:00
}
2017-04-22 05:08:29 +12:00
Console.WriteLine("Folder created.");
}
2014-01-13 05:05:31 +13:00
private static void CopyFFmpeg(string destination, bool include32bit, bool include64bit)
2016-08-28 05:57:46 +12:00
{
if (include32bit)
{
if (!File.Exists(FFmpeg32bit))
{
2020-09-06 23:03:16 +12:00
string filePath = SetupHelpers.DownloadFile("https://github.com/ShareX/FFmpeg/releases/download/v4.3.1/ffmpeg-4.3.1-win32.zip");
ZipManager.Extract(filePath, ".", false, entry => entry.Name.Equals("ffmpeg.exe", StringComparison.OrdinalIgnoreCase), 100_000_000);
File.Move("ffmpeg.exe", FFmpeg32bit);
}
SetupHelpers.CopyFile(FFmpeg32bit, destination);
}
if (include64bit)
{
if (!File.Exists(FFmpeg64bit))
{
2020-09-06 23:03:16 +12:00
string filePath = SetupHelpers.DownloadFile("https://github.com/ShareX/FFmpeg/releases/download/v4.3.1/ffmpeg-4.3.1-win64.zip");
ZipManager.Extract(filePath, ".", false, entry => entry.Name.Equals("ffmpeg.exe", StringComparison.OrdinalIgnoreCase), 100_000_000);
File.Move("ffmpeg.exe", FFmpeg64bit);
}
SetupHelpers.CopyFile(FFmpeg64bit, destination);
}
2016-08-28 05:57:46 +12:00
}
private static void OpenOutputDirectory()
{
2016-08-27 06:34:35 +12:00
Process.Start(OutputDir);
}
private static void UploadLatestFile()
{
FileInfo fileInfo = new DirectoryInfo(OutputDir).GetFiles("*.exe").OrderByDescending(f => f.LastWriteTime).FirstOrDefault();
2016-08-27 06:34:35 +12:00
if (fileInfo != null)
{
Console.WriteLine("Uploading setup file.");
2016-08-27 12:02:43 +12:00
Process.Start(DebugExecutablePath, fileInfo.FullName);
}
}
}
}