forked from kendarorg/RepositoryCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
169 lines (144 loc) · 5.77 KB
/
Program.cs
File metadata and controls
169 lines (144 loc) · 5.77 KB
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
using MultiRepositories;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using Newtonsoft.Json;
using MultiRepositories.Repositories;
using System.Windows.Forms;
using System.Drawing;
using static System.Net.Mime.MediaTypeNames;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using System.Reflection;
using Ioc;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
namespace RepositoryCache
{
class Program
{
static NotifyIcon notifyIcon = new NotifyIcon();
static bool Visible = true;
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void SetConsoleWindowVisibility(bool visible)
{
IntPtr hWnd = FindWindow(null, Console.Title);
if (hWnd != IntPtr.Zero)
{
if (visible) ShowWindow(hWnd, 1); //1 = SW_SHOWNORMAL
else ShowWindow(hWnd, 0); //0 = SW_HIDE
}
}
static void Main(string[] args)
{
var options = new Options();
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(opts => RunOptionsAndReturnExitCode(opts))
.WithNotParsed<Options>((errs) => HandleParseError(errs));
}
private static void HandleParseError(IEnumerable<Error> errs)
{
}
static public string AssemblyDirectory
{
get
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
var assemblyDir = Path.GetDirectoryName(path);
return assemblyDir;
}
}
private static void RunOptionsAndReturnExitCode(Options opts)
{
var settingsFile = opts.Settings;
if (!string.IsNullOrWhiteSpace(settingsFile) && File.Exists(settingsFile))
{
Console.WriteLine("Reading settings from " + settingsFile);
opts = JsonConvert.DeserializeObject<Options>(File.ReadAllText(settingsFile));
}
if (string.IsNullOrWhiteSpace(opts.Path))
{
opts.Path = Directory.GetCurrentDirectory();
}
opts.Settings = settingsFile;
var settings = JsonConvert.SerializeObject(opts);
Console.WriteLine(settings);
if (opts.ShowInTray)
{
notifyIcon.DoubleClick += (s, e) =>
{
Visible = !Visible;
SetConsoleWindowVisibility(Visible);
};
//notifyIcon.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
Bitmap bmp = new Bitmap(16, 16, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.FillEllipse(Brushes.Red, 0, 0, 16, 16);
g.FillRectangle(Brushes.White, 4, 6, 8, 4);
}
notifyIcon.Icon = FlimFlan.IconEncoder.Converter.BitmapToIcon(bmp);
notifyIcon.Visible = true;
notifyIcon.Text = "RepositoryCache";
var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Exit", null, (s, e) => { System.Windows.Forms.Application.Exit(); });
notifyIcon.ContextMenuStrip = contextMenu;
SetConsoleWindowVisibility(false);
}
if (string.IsNullOrWhiteSpace(opts.Host))
{
opts.Host = "http://localhost:"+opts.Port;
}
var container = new WindsorContainer();
RegisterApps(container, new AppProperties(opts.Host,"db"));
var shttp= container.Resolve<ISimpleHTTPServer>();
shttp.Start(opts.Path, opts.Port, opts.LogRequests, opts.Urls, opts.Ignores);
if (opts.ShowInTray)
{
System.Windows.Forms.Application.Run();
}
else
{
while (Console.ReadKey().KeyChar != 'q')
{
Console.WriteLine("");
continue;
}
}
shttp.Stop();
}
private static void RegisterApps(WindsorContainer container, AppProperties appProperties)
{
//container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
container.Kernel.Resolver.AddSubResolver(new ListResolver(container.Kernel,true));
container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel,true));
var filter = new AssemblyFilter(AssemblyDirectory);
container.Register(
Component.For<AppProperties>().Instance(appProperties));
/*container.Register(
Classes.FromAssemblyInDirectory(filter)
.BasedOn<AppProperties>());*/
container.Register(
Classes.FromAssemblyInDirectory(filter)
.BasedOn<ISingleton>()
.LifestyleSingleton()
.WithServiceAllInterfaces());
container.Register(
Classes.FromAssemblyInDirectory(filter)
.BasedOn<ITransient>()
.LifestyleTransient()
.WithServiceAllInterfaces());
}
}
}