-
Notifications
You must be signed in to change notification settings - Fork 541
/
IOSFeed.cs
146 lines (128 loc) · 5.7 KB
/
IOSFeed.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
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
namespace AdaptiveCards.Tools.IOSFeed
{
public static class Constants
{
public const string ConnectionStringPath = "source/ios/tools/IOSFeed/ConnectString.txt";
public const string ContainerId = "adaptivecardsiosblobs";
public const string FrameworkPath = "source/ios/AdaptiveCards/AdaptiveCards";
public const string FrameworkName = "AdaptiveCards.framework.zip";
public const string PodspecPath = "./source/ios/tools/";
public const string TargetPodspecPath = "./source/ios/";
public const string PodspecName = "AdaptiveCards.podspec";
}
/// <summary>
/// IOSFeed picks up ios artifact, sends to the Azure storage
/// and updates podspec for pod publication
/// </summary>
class IOSFeed
{
public static void Main()
{
// Run the examples asynchronously, wait for the results before proceeding
ProcessAsync().GetAwaiter().GetResult();
}
private static async Task ProcessAsync()
{
string storageConnectionString;
FileInfo fi = new FileInfo(Constants.ConnectionStringPath);
if (!fi.Exists)
{
throw new InvalidOperationException("connection string file does not exist");
}
using (StreamReader sr = File.OpenText(Constants.ConnectionStringPath))
{
storageConnectionString = sr.ReadToEnd();
}
// Check whether the connection string can be parsed.
CloudStorageAccount storageAccount;
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
// If the connection string is valid, proceed with operations against Blob
// storage here.
// Create the CloudBlobClient that represents the
// Blob storage endpoint for the storage account.
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer =
cloudBlobClient.GetContainerReference(Constants.ContainerId);
await cloudBlobContainer.CreateIfNotExistsAsync();
// Set the permissions so the blobs are public.
BlobContainerPermissions permissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
await cloudBlobContainer.SetPermissionsAsync(permissions);
var sourceFile = Path.Combine(Constants.FrameworkPath, Constants.FrameworkName);
var blobGuid = Guid.NewGuid().ToString();
var cloudFileName = blobGuid + Constants.FrameworkName;
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(cloudFileName);
await cloudBlockBlob.UploadFromFileAsync(sourceFile);
BlobContinuationToken blobContinuationToken = null;
do
{
var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken);
// Get the value of the continuation token returned by the listing call.
blobContinuationToken = results.ContinuationToken;
foreach (IListBlobItem item in results.Results)
{
var uriString = item.Uri.ToString();
if (uriString.Contains(blobGuid))
{
UpdatePodSpec(uriString);
}
}
} while (blobContinuationToken != null);
}
else
{
throw new InvalidOperationException("invalid connection string");
}
}
private static void UpdatePodSpec(string uri)
{
var sourceFile = Path.Combine(Constants.PodspecPath, Constants.PodspecName);
var targetFile = Path.Combine(Constants.TargetPodspecPath, Constants.PodspecName);
FileInfo fi = new FileInfo(sourceFile);
if (!fi.Exists)
{
throw new FileNotFoundException("missing podspec");
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(sourceFile))
{
string s = "";
string output = "";
while ((s = sr.ReadLine()) != null)
{
if (s.Length != 0)
{
var splits = s.Split('=');
if (splits.Length > 0)
{
if (splits[0].Contains("spec.source"))
{
s = splits[0] + "= { :http => " + "'" + uri + "' }";
}
else if (splits[0].Contains("spec.version"))
{
var adaptiveVersion = Environment.GetEnvironmentVariable("ADCVERSION");
s = splits[0] + "= '" + adaptiveVersion + "'" ;
}
}
output += (s + "\n");
} else
{
output += "\n";
}
}
File.WriteAllText(targetFile, output);
}
}
}
}