Skip to content

Commit

Permalink
add support for creating link to onedrive config
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Aug 25, 2022
1 parent ee5f9a2 commit b568d65
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
21 changes: 20 additions & 1 deletion DirLinkerConfig/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ namespace DirLinkerConfig
public static class DirLinkerInfo
{
public const string ApplyConfigArgument = "--applyConfig";
public const string CreateConfigLink = "--createConfigLink";
public const string HaltOnErrorOnly = "--haltOnErrorOnly";

public static void MkDirs(this FileInfo file)
{
if (!file.Directory?.Exists ?? false)
file.Directory.MkDirs();
}

public static void MkDirs(this DirectoryInfo dir)
{
if (!dir.Parent?.Exists ?? false)
dir.Create();
}
}

public class Configuration : IUpdateable<Configuration>
Expand All @@ -26,7 +39,13 @@ public class Configuration : IUpdateable<Configuration>
static Configuration()
{
DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "org.comroid");
ConfigFile = Path.Combine(DataDir, "dirLinker.json");
ConfigFile = Path.Combine(DataDir,
#if DEBUG
"dirLinker-debug.json"
#else
"dirLinker.json"
#endif
);
Directory.CreateDirectory(DataDir);
}

Expand Down
1 change: 1 addition & 0 deletions DirLinkerConfig/DirLinkerConfig.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>0.2.0</Version>
<AssemblyVersion>0.1.3</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
66 changes: 63 additions & 3 deletions DirLinkerWPF/DirLinker.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public DirLinker()
if (!File.Exists(Configuration.ConfigFile))
{
// ask the user to use config from a host; e.g. onedrive
// todo
TryLinkConfig();
}

Configuration.LoadConfig(this);
CleanupConfig();
}
Expand All @@ -55,6 +54,67 @@ private void UpdateUI()
ReloadView();
}

private void TryLinkConfig()
{
Window[] window = new Window[1];
Button yesBtn = new Button()
{
Content = "Yes"
};
Button noBtn = new Button()
{
Content = "No"
};
yesBtn.Click += (sender, args) =>
{
CreateOneDriveConfigLink();
window[0].Close();
};
noBtn.Click += (sender, args) => window[0].Close();
window[0] = new Window()
{
Height = 360,
Width = 800,
Title = "DirLinker - Create configuration link?",
Content = new StackPanel()
{
Orientation = Orientation.Vertical,
Children =
{
new TextBlock()
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
TextAlignment = TextAlignment.Center,
Text = "Do you want to link configuration to OneDrive?"
},
new StackPanel()
{
Orientation = Orientation.Horizontal,
Children =
{
yesBtn,
noBtn
}
}
}
}
};
window[0].ShowDialog();
}

private void CreateOneDriveConfigLink()
{
var startInfo = new ProcessStartInfo
{
FileName = "HardLinkTool.exe",
Arguments = DirLinkerInfo.CreateConfigLink + ' ' + DirLinkerInfo.HaltOnErrorOnly,
UseShellExecute = true,
Verb = "runas"
};
Process.Start(startInfo)!.WaitForExit();
}

private void ApplyConfigToOS()
{
var startInfo = new ProcessStartInfo
Expand Down Expand Up @@ -138,7 +198,7 @@ private void PromptText(object line)
new Window
{
Height = 260,
Width = 800,
Width = 1200,
Content = new TextBlock
{
HorizontalAlignment = HorizontalAlignment.Stretch,
Expand Down
1 change: 1 addition & 0 deletions DirLinkerWPF/DirLinkerWPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ApplicationIcon>logo-clean.ico</ApplicationIcon>
<StartupObject>DirLinkerWPF.App</StartupObject>
<Version>0.1.1</Version>
<AssemblyVersion>0.1.3</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
24 changes: 23 additions & 1 deletion HardLinkTool/HardLinkTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,29 @@ private static void WaitForUserInput()

private static void Run(string[] args)
{
if (args.Contains(DirLinkerInfo.ApplyConfigArgument))
if (args.Contains(DirLinkerInfo.CreateConfigLink))
{
try
{
var targetFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "OneDrive", "config",
"org.comroid");
var target = new FileInfo(targetFile);
if (target.Exists && target.IsSymbolicLink() && target.IsSymbolicLinkValid())
{
Console.WriteLine("Skipping creation of config symlink because a symlink already exists");
return;
}
target.MkDirs();
target.Create();
Console.WriteLine($"Creating configuration link from {Configuration.ConfigFile} to {targetFile}");
target.CreateSymbolicLink(Configuration.ConfigFile);
}
catch (UnauthorizedAccessException _)
{
}
}
else if (args.Contains(DirLinkerInfo.ApplyConfigArgument))
{
Configuration.LoadConfig();
ApplyConfig();
Expand Down
1 change: 1 addition & 0 deletions HardLinkTool/HardLinkTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<StartupObject>HardLinkTool.HardLinkTool</StartupObject>
<Version>0.1.0</Version>
<AssemblyVersion>0.1.3</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit b568d65

Please sign in to comment.