2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using Paradox.Framework.Diagnostics;
7 using Paradox.Framework.Serialization;
9 namespace Paradox.BuildEngine
13 private static readonly
string AdbExecutable = FindAdbExecutable();
17 var sdcardPath = RunAdb(device,
"shell \"echo $EXTERNAL_STORAGE\"");
23 var devices = RunAdb(null,
"devices");
27 return devices.Skip(1).Select(x => x.Split(
' ',
'\t').First()).ToArray();
38 public static void Synchronize(
Logger logger,
string device, Dictionary<string, string> fileMapping,
string androidPath,
string cacheFile)
41 if (!androidPath.EndsWith(
"/"))
42 androidPath = androidPath +
"/";
45 var currentVersions = fileMapping
46 .ToDictionary(x => x.Key, x =>
new FileVersion(x.Value));
49 var previousVersions =
new Dictionary<string, FileVersion>();
52 using (var file = File.OpenRead(cacheFile))
54 var binaryReader =
new BinarySerializationReader(file);
55 binaryReader.Serialize(ref previousVersions, ArchiveMode.Deserialize);
62 var filesToRemove =
new List<string>();
63 var filesToUpload =
new List<string>();
66 foreach (var file
in previousVersions.Where(x => !currentVersions.ContainsKey(x.Key)))
68 filesToRemove.Add(file.Key);
72 foreach (var file
in currentVersions)
75 if (!previousVersions.TryGetValue(file.Key, out fileVersion)
76 || fileVersion.FileSize != file.Value.FileSize
77 || fileVersion.LastModifiedDate != file.Value.LastModifiedDate)
79 filesToUpload.Add(file.Key);
84 foreach (var file
in filesToUpload)
87 logger.Verbose(
"Copying file {0}", file);
88 RunAdb(device,
string.Format(
"push \"{0}\" \"{1}\"", fileMapping[file], androidPath + file.Replace(
'\\',
'/')));
92 foreach (var file
in filesToRemove)
95 logger.Verbose(
"Deleting file {0}", file);
96 RunAdb(device,
string.Format(
"shell \"rm {0}\"", androidPath + file.Replace(
'\\',
'/')));
100 using (var file = File.Create(cacheFile))
102 var binaryWriter =
new BinarySerializationWriter(file);
103 binaryWriter.Write(currentVersions);
107 private static string FindAdbExecutable()
109 var androidSdkDir = Environment.GetEnvironmentVariable(
"ANDROID_SDK");
110 var androidAdbExecutable = androidSdkDir != null ? androidSdkDir +
@"\platform-tools\adb" :
"adb";
112 return androidAdbExecutable;
115 private static string[] RunAdb(
string device,
string arguments)
119 arguments =
"-s " + device +
' ' + arguments;
121 var processStartInfo =
new ProcessStartInfo()
123 FileName = AdbExecutable,
124 Arguments = arguments,
125 UseShellExecute =
false,
126 CreateNoWindow =
true,
127 RedirectStandardOutput =
true,
130 var lines =
new List<string>();
132 var adbProcess = Process.Start(processStartInfo);
133 adbProcess.OutputDataReceived += (sender, args) =>
135 if (!
string.IsNullOrEmpty(args.Data))
139 lines.Add(args.Data);
143 adbProcess.BeginOutputReadLine();
144 adbProcess.WaitForExit();
146 return lines.ToArray();
157 LastModifiedDate = DateTime.MinValue;
160 if (
File.Exists(fileName))
162 var fileInfo =
new FileInfo(fileName);
163 LastModifiedDate = fileInfo.LastWriteTime;
164 FileSize = fileInfo.Length;
static string GetExternalStoragePath(string device)
static string[] GetDevices()
static void Synchronize(Logger logger, string device, Dictionary< string, string > fileMapping, string androidPath, string cacheFile)
Synchronizes files to an android device.
DateTime LastModifiedDate
FileVersion(string fileName)