-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
153 lines (135 loc) · 5.9 KB
/
Program.cs
File metadata and controls
153 lines (135 loc) · 5.9 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
using System;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Web.Deployment;
namespace WebDeployment
{
public static class MethodExtention
{
public static string SafeGetAttribute(this XElement node, string attribute, string defaultValue = null)
{
var attr = node.Attribute(attribute);
return attr == null ? defaultValue : attr.Value;
}
}
public enum ContentType
{
Pacakge,
Folder,
File
}
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Error.WriteLine("Usage: {0} <publishSettings> <source>", Path.GetFileName(Assembly.GetEntryAssembly().Location));
Environment.Exit(1);
}
var publishSettingsPath = args[0];
var sourcePath = args[1];
ContentType contentType = ContentType.File;
if (!File.Exists(publishSettingsPath))
{
Console.Error.WriteLine("{0}: Not found.", publishSettingsPath);
Environment.Exit(1);
}
if (Directory.Exists(sourcePath))
{
contentType = ContentType.Folder;
}
else if (!File.Exists(sourcePath))
{
Console.Error.WriteLine("{0}: Not found.", sourcePath);
Environment.Exit(1);
}
else if (Path.GetExtension(sourcePath).Equals(".zip", StringComparison.InvariantCultureIgnoreCase))
{
contentType = ContentType.Pacakge;
}
var document = XElement.Load(publishSettingsPath);
var profile = document.XPathSelectElement("//publishProfile[@publishMethod='MSDeploy']");
if (profile == null)
{
Console.Error.WriteLine("{0}: Not a valid publishing profile.", publishSettingsPath);
Environment.Exit(1);
}
var publishUrl = profile.SafeGetAttribute("publishUrl");
var destinationAppUrl = profile.SafeGetAttribute("destinationAppUrl");
var userName = profile.SafeGetAttribute("userName");
var password = profile.SafeGetAttribute("userPWD");
var siteName = profile.SafeGetAttribute("msdeploySite");
// Database related attributes
var databaseInfo = new SqlConnectionStringBuilder();
var databaseSection = profile.XPathSelectElements("./databases/add").FirstOrDefault();
if (databaseSection != null)
{
databaseInfo.ConnectionString = databaseSection.SafeGetAttribute("connectionString");
}
var webDeployServer = string.Format(@"https://{0}/msdeploy.axd?site={1}", publishUrl, siteName);
Console.WriteLine("Publishing {0} to {1}", sourcePath, destinationAppUrl);
// Set up deployment
var sourceProvider = contentType == ContentType.Pacakge ? DeploymentWellKnownProvider.Package : DeploymentWellKnownProvider.ContentPath;
var destinationProvider = contentType == ContentType.Pacakge ? DeploymentWellKnownProvider.Auto : DeploymentWellKnownProvider.ContentPath;
var sourceOptions = new DeploymentBaseOptions();
var destinationOptions = new DeploymentBaseOptions
{
ComputerName = webDeployServer,
UserName = userName,
Password = password,
AuthenticationType = "basic",
IncludeAcls = true,
TraceLevel = System.Diagnostics.TraceLevel.Info
};
destinationOptions.Trace += (sender, e) => Console.WriteLine(e.Message);
var destinationPath = siteName;
if (contentType == ContentType.File)
{
var filename = new FileInfo(sourcePath).Name;
destinationPath += "/" + filename;
}
var syncOptions = new DeploymentSyncOptions { DoNotDelete = true }; // Please change as you want
// Start deployment
using (var deploy = DeploymentManager.CreateObject(sourceProvider, sourcePath, sourceOptions))
{
// Apply package parameters
foreach (var p in deploy.SyncParameters)
{
switch (p.Name)
{
case "IIS Web Application Name":
case "AppPath":
p.Value = siteName;
break;
case "DbServer":
p.Value = databaseInfo.DataSource;
break;
case "DbName":
p.Value = databaseInfo.InitialCatalog;
break;
case "DbUsername":
case "DbAdminUsername":
p.Value = databaseInfo.UserID;
break;
case "DbPassword":
case "DbAdminPassword":
p.Value = databaseInfo.Password;
break;
}
}
var changeSummary = deploy.SyncTo(destinationProvider, destinationPath, destinationOptions, syncOptions);
Console.WriteLine("Deployment finsihed.");
Console.WriteLine("Added: " + changeSummary.ObjectsAdded);
Console.WriteLine("Updated: " + changeSummary.ObjectsUpdated);
Console.WriteLine("Deleted: " + changeSummary.ObjectsDeleted);
Console.WriteLine("Total errors: " + changeSummary.Errors);
Console.WriteLine("Total changes: " + changeSummary.TotalChanges);
}
}
}
}