forked from DotNetOpenAuth/DotNetOpenAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepareOhlohRelease.cs
138 lines (116 loc) · 4.31 KB
/
PrepareOhlohRelease.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//-----------------------------------------------------------------------
// <copyright file="PrepareOhlohRelease.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.BuildTasks {
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
/// <summary>
/// Creates an Ohloh.net upload instruct XLM file and bash script that uses it.
/// </summary>
public class PrepareOhlohRelease : Task {
/// <summary>
/// Initializes a new instance of the <see cref="PrepareOhlohUpload"/> class.
/// </summary>
public PrepareOhlohRelease() {
}
[Required]
public string Release { get; set; }
public string ReleaseNotes { get; set; }
[Required]
public string InstructFile { get; set; }
[Required]
public string UploadScript { get; set; }
[Required]
public string OhlohUser { get; set; }
[Required]
public string OhlohProject { get; set; }
[Required]
public ITaskItem[] RedistributableFiles { get; set; }
public override bool Execute() {
this.WriteInstructFile();
this.WriteUploadScript();
return !this.Log.HasLoggedErrors;
}
private void WriteInstructFile() {
var packages = from redist in this.RedistributableFiles
let file = new { Path = redist.ItemSpec, Package = redist.GetMetadata("Package"), Platform = redist.GetMetadata("Platform"), Icon = redist.GetMetadata("Icon") }
group file by file.Package into package
select new XElement(
"package",
new XAttribute("name", package.Key),
new XElement(
"releases",
new XElement(
"release",
new XAttribute("name", this.Release),
new XAttribute("date", XmlConvert.ToString(DateTime.Now)),
new XElement("notes", this.ReleaseNotes),
new XElement(
"files",
package.Select(
f => new XElement(
"file",
new XAttribute("name", Path.GetFileName(f.Path)),
new XAttribute("date", XmlConvert.ToString(DateTime.Now)),
new XAttribute("platform", f.Platform),
new XAttribute("icon", f.Icon)
)
)
)
)
)
);
var instructXml = new XElement("packages", packages);
var writerSettings = new XmlWriterSettings {
OmitXmlDeclaration = true,
Indent = true,
IndentChars = "\t",
};
using (var writer = XmlWriter.Create(this.InstructFile, writerSettings)) {
instructXml.Save(writer);
}
this.Log.LogMessage("Ohloh instruct file written to: \"{0}\"", this.InstructFile);
}
private void WriteUploadScript() {
int longestPath = Math.Max(
GetLinuxPath(Path.GetFullPath(this.InstructFile)).Length,
this.RedistributableFiles.Max(f => GetLinuxPath(f.GetMetadata("FullPath")).Length));
using (StreamWriter writer = new StreamWriter(this.UploadScript)) {
writer.WriteLine("#!/bin/bash");
writer.WriteLine();
foreach (var file in this.RedistributableFiles) {
writer.WriteLine("scp {0,-" + longestPath + "} {1}@upload.ohloh.net:{2}/files", GetLinuxPath(file.GetMetadata("FullPath")), this.OhlohUser, this.OhlohProject);
}
writer.WriteLine();
writer.WriteLine("scp {0,-" + longestPath + "} {1}@upload.ohloh.net:{2}/instructs", GetLinuxPath(Path.GetFullPath(this.InstructFile)), this.OhlohUser, this.OhlohProject);
writer.WriteLine();
writer.WriteLine("# Download the instruct log by executing this command:");
writer.WriteLine("# scp {0}@upload.ohloh.net:{1}/logs/upload.log .", this.OhlohUser, this.OhlohProject);
}
this.Log.LogMessage("Ohloh upload script written to: \"{0}\".", this.UploadScript);
}
private static string GetLinuxPath(string windowsPath) {
if (String.IsNullOrEmpty(windowsPath)) {
throw new ArgumentNullException("windowsPath");
}
string linuxPath = Regex.Replace(
windowsPath,
@"^([A-Za-z])\:",
m => "/" + m.Groups[1].Value)
.Replace('\\', '/')
.Replace(" ", "\\ ");
return linuxPath;
}
}
}