-
Notifications
You must be signed in to change notification settings - Fork 72
/
ProjectFactory.cs
1099 lines (945 loc) · 44 KB
/
ProjectFactory.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Xml.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using NuGet.Common;
namespace NuGet.Commands
{
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
internal class ProjectFactory : IPropertyProvider
{
private readonly Project _project;
private ILogger _logger;
private ISettings _settings;
// Files we want to always exclude from the resulting package
private static readonly HashSet<string> _excludeFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
Constants.PackageReferenceFile,
"Web.Debug.config",
"Web.Release.config"
};
private readonly Dictionary<string, string> _properties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// Packaging folders
private const string ContentFolder = "content";
private const string ReferenceFolder = "lib";
private const string ToolsFolder = "tools";
private const string SourcesFolder = "src";
// Common item types
private const string SourcesItemType = "Compile";
private const string ContentItemType = "Content";
private const string ProjectReferenceItemType = "ProjectReference";
private const string NuGetConfig = "nuget.config";
private const string PackagesFolder = "packages";
private const string TransformFileExtension = ".transform";
[Import]
public IMachineWideSettings MachineWideSettings { get; set; }
public ProjectFactory(string path, IDictionary<string, string> projectProperties)
: this(new Project(path, projectProperties, null))
{
}
public ProjectFactory(Project project)
{
_project = project;
ProjectProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
AddSolutionDir();
_settings = null;
// Get the target framework of the project
string targetFrameworkMoniker = _project.GetPropertyValue("TargetFrameworkMoniker");
if (!String.IsNullOrEmpty(targetFrameworkMoniker))
{
TargetFramework = new FrameworkName(targetFrameworkMoniker);
}
}
private ISettings DefaultSettings
{
get
{
if (null == _settings)
{
var physicalFileSystem = new PhysicalFileSystem(_project.DirectoryPath);
physicalFileSystem.Logger = _logger;
_settings = Settings.LoadDefaultSettings(
physicalFileSystem,
null,
MachineWideSettings);
}
return _settings;
}
}
private string TargetPath
{
get;
set;
}
private FrameworkName TargetFramework
{
get;
set;
}
public bool IncludeSymbols { get; set; }
public bool IncludeReferencedProjects { get; set; }
public bool Build { get; set; }
public Dictionary<string, string> ProjectProperties { get; private set; }
public bool IsTool { get; set; }
public ILogger Logger
{
get
{
return _logger ?? NullLogger.Instance;
}
set
{
_logger = value;
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to continue regardless of any error we encounter extracting metadata.")]
public PackageBuilder CreateBuilder(string basePath)
{
BuildProject();
Logger.Log(MessageLevel.Info, LocalizedResourceManager.GetString("PackagingFilesFromOutputPath"), Path.GetDirectoryName(TargetPath));
var builder = new PackageBuilder();
try
{
// Populate the package builder with initial metadata from the assembly/exe
AssemblyMetadataExtractor.ExtractMetadata(builder, TargetPath);
}
catch (Exception ex)
{
Logger.Log(MessageLevel.Warning, LocalizedResourceManager.GetString("UnableToExtractAssemblyMetadata"), Path.GetFileName(TargetPath));
IConsole console = Logger as IConsole;
if (console != null && console.Verbosity == Verbosity.Detailed)
{
console.Log(MessageLevel.Warning, ex.ToString());
}
ExtractMetadataFromProject(builder);
}
var projectAuthor = InitializeProperties(builder);
// If the package contains a nuspec file then use it for metadata
Manifest manifest = ProcessNuspec(builder, basePath);
// Remove the extra author
if (builder.Authors.Count > 1)
{
builder.Authors.Remove(projectAuthor);
}
// Add output files
ApplyAction(p => p.AddOutputFiles(builder));
// if there is a .nuspec file, only add content files if the <files /> element is not empty.
if (manifest == null || manifest.Files == null || manifest.Files.Count > 0)
{
// Add content files
ApplyAction(p => p.AddFiles(builder, ContentItemType, ContentFolder));
}
// Add sources if this is a symbol package
if (IncludeSymbols)
{
ApplyAction(p => p.AddFiles(builder, SourcesItemType, SourcesFolder));
}
ProcessDependencies(builder);
// Set defaults if some required fields are missing
if (String.IsNullOrEmpty(builder.Description))
{
builder.Description = "Description";
Logger.Log(MessageLevel.Warning, LocalizedResourceManager.GetString("Warning_UnspecifiedField"), "Description", "Description");
}
if (!builder.Authors.Any())
{
builder.Authors.Add(Environment.UserName);
Logger.Log(MessageLevel.Warning, LocalizedResourceManager.GetString("Warning_UnspecifiedField"), "Author", Environment.UserName);
}
return builder;
}
internal string InitializeProperties(IPackageMetadata metadata)
{
// Set the properties that were resolved from the assembly/project so they can be
// resolved by name if the nuspec contains tokens
_properties.Clear();
_properties.Add("Id", metadata.Id);
_properties.Add("Version", metadata.Version.ToString());
if (!String.IsNullOrEmpty(metadata.Title))
{
_properties.Add("Title", metadata.Title);
}
if (!String.IsNullOrEmpty(metadata.Description))
{
_properties.Add("Description", metadata.Description);
}
if (!String.IsNullOrEmpty(metadata.Copyright))
{
_properties.Add("Copyright", metadata.Copyright);
}
string projectAuthor = metadata.Authors.FirstOrDefault();
if (!String.IsNullOrEmpty(projectAuthor))
{
_properties.Add("Author", projectAuthor);
}
return projectAuthor;
}
dynamic IPropertyProvider.GetPropertyValue(string propertyName)
{
string value;
if (!_properties.TryGetValue(propertyName, out value))
{
ProjectProperty property = _project.GetProperty(propertyName);
if (property != null)
{
value = property.EvaluatedValue;
}
}
return value;
}
private void BuildProject()
{
if (Build)
{
if (TargetFramework != null)
{
Logger.Log(
MessageLevel.Info,
LocalizedResourceManager.GetString("BuildingProjectTargetingFramework"),
_project.FullPath,
TargetFramework);
}
using (var projectCollection = new ProjectCollection(ToolsetDefinitionLocations.Registry | ToolsetDefinitionLocations.ConfigurationFile))
{
BuildRequestData requestData = new BuildRequestData(_project.FullPath, ProjectProperties, _project.ToolsVersion, new string[0], null);
var parameters = new BuildParameters(projectCollection)
{
Loggers = new[] { new ConsoleLogger { Verbosity = LoggerVerbosity.Quiet }},
NodeExeLocation = typeof(ProjectFactory).Assembly.Location,
ToolsetDefinitionLocations = projectCollection.ToolsetLocations
};
BuildResult result = BuildManager.DefaultBuildManager.Build(parameters, requestData);
// Build the project so that the outputs are created
if (result.OverallResult == BuildResultCode.Failure)
{
// If the build fails, report the error
throw new CommandLineException(LocalizedResourceManager.GetString("FailedToBuildProject"), Path.GetFileName(_project.FullPath));
}
TargetPath = ResolveTargetPath(result);
}
}
else
{
TargetPath = ResolveTargetPath();
// Make if the target path doesn't exist, fail
if (!File.Exists(TargetPath))
{
throw new CommandLineException(LocalizedResourceManager.GetString("UnableToFindBuildOutput"), TargetPath);
}
}
}
private string ResolveTargetPath()
{
// Set the project properties
foreach (var property in ProjectProperties)
{
var existingProperty = _project.GetProperty(property.Key);
if (existingProperty == null || !IsGlobalProperty(existingProperty))
{
// Only set the property if it's not already defined as a global property
// (which those passed in via the ctor are) as trying to set global properties
// with this method throws.
_project.SetProperty(property.Key, property.Value);
}
}
// Re-evaluate the project so that the new property values are applied
_project.ReevaluateIfNecessary();
// Return the new target path
return _project.GetPropertyValue("TargetPath");
}
private static bool IsGlobalProperty(ProjectProperty projectProperty)
{
// This property isn't available on xbuild (mono)
var property = typeof(ProjectProperty).GetProperty("IsGlobalProperty", BindingFlags.Public | BindingFlags.Instance);
if(property != null)
{
return (bool)property.GetValue(projectProperty, null);
}
// REVIEW: Maybe there's something better we can do on mono
// Just return false if the property isn't there
return false;
}
private string ResolveTargetPath(BuildResult result)
{
string targetPath = null;
TargetResult targetResult;
if (result.ResultsByTarget.TryGetValue("Build", out targetResult))
{
if (targetResult.Items.Any())
{
targetPath = targetResult.Items.First().ItemSpec;
}
}
return targetPath ?? ResolveTargetPath();
}
private void ExtractMetadataFromProject(PackageBuilder builder)
{
builder.Id = builder.Id ??
_project.GetPropertyValue("AssemblyName") ??
Path.GetFileNameWithoutExtension(_project.FullPath);
string version = _project.GetPropertyValue("Version");
builder.Version = builder.Version ??
SemanticVersion.ParseOptionalVersion(version) ??
new SemanticVersion("1.0");
}
private static IEnumerable<string> GetFiles(string path, string fileNameWithoutExtension, HashSet<string> allowedExtensions)
{
return allowedExtensions.Select(extension => Directory.GetFiles(path, fileNameWithoutExtension + extension)).SelectMany(a => a);
}
private void ApplyAction(Action<ProjectFactory> action)
{
if (IncludeReferencedProjects)
{
RecursivelyApply(action);
}
else
{
action(this);
}
}
/// <summary>
/// Recursively execute the specified action on the current project and
/// projects referenced by the current project.
/// </summary>
/// <param name="action">The action to be executed.</param>
private void RecursivelyApply(Action<ProjectFactory> action)
{
using (var projectCollection = new ProjectCollection())
{
RecursivelyApply(action, projectCollection);
}
}
/// <summary>
/// Recursively execute the specified action on the current project and
/// projects referenced by the current project.
/// </summary>
/// <param name="action">The action to be executed.</param>
/// <param name="alreadyAppliedProjects">The collection of projects that have been processed.
/// It is used to avoid processing the same project more than once.</param>
private void RecursivelyApply(Action<ProjectFactory> action, ProjectCollection alreadyAppliedProjects)
{
action(this);
foreach (var item in _project.GetItems(ProjectReferenceItemType))
{
string fullPath = item.GetMetadataValue("FullPath");
if (!string.IsNullOrEmpty(fullPath) &&
!NuspecFileExists(fullPath) &&
alreadyAppliedProjects.GetLoadedProjects(fullPath).IsEmpty())
{
var project = new Project(
fullPath,
globalProperties: null,
toolsVersion: null,
projectCollection: alreadyAppliedProjects);
var referencedProject = new ProjectFactory(project);
referencedProject.Logger = _logger;
referencedProject.IncludeSymbols = IncludeSymbols;
referencedProject.Build = Build;
referencedProject.IncludeReferencedProjects = IncludeReferencedProjects;
referencedProject.ProjectProperties = ProjectProperties;
referencedProject.TargetFramework = TargetFramework;
referencedProject.BuildProject();
referencedProject.RecursivelyApply(action, alreadyAppliedProjects);
}
}
}
/// <summary>
/// Returns whether a project file has a corresponding nuspec file.
/// </summary>
/// <param name="projectFileFullName">The name of the project file.</param>
/// <returns>True if there is a corresponding nuspec file.</returns>
private static bool NuspecFileExists(string projectFileFullName)
{
var nuspecFile = Path.ChangeExtension(projectFileFullName, Constants.ManifestExtension);
return File.Exists(nuspecFile);
}
/// <summary>
/// Adds referenced projects that have corresponding nuspec files as dependencies.
/// </summary>
/// <param name="dependencies">The dependencies collection where the new dependencies
/// are added into.</param>
private void AddProjectReferenceDependencies(Dictionary<string, PackageDependency> dependencies)
{
var processedProjects = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var projectsToProcess = new Queue<Project>();
using (var projectCollection = new ProjectCollection())
{
projectsToProcess.Enqueue(_project);
while (projectsToProcess.Count > 0)
{
var project = projectsToProcess.Dequeue();
processedProjects.Add(project.FullPath);
foreach (var projectReference in project.GetItems(ProjectReferenceItemType))
{
string fullPath = projectReference.GetMetadataValue("FullPath");
if (string.IsNullOrEmpty(fullPath) ||
processedProjects.Contains(fullPath))
{
continue;
}
var loadedProjects = projectCollection.GetLoadedProjects(fullPath);
var referencedProject = loadedProjects.Count > 0 ?
loadedProjects.First() :
new Project(
fullPath,
globalProperties: project.GlobalProperties,
toolsVersion: null,
projectCollection: projectCollection);
if (NuspecFileExists(fullPath))
{
var dependency = CreateDependencyFromProject(referencedProject);
dependencies[dependency.Id] = dependency;
}
else
{
projectsToProcess.Enqueue(referencedProject);
}
}
}
}
}
// Creates a package dependency from the given project, which has a corresponding
// nuspec file.
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to continue regardless of any error we encounter extracting metadata.")]
private PackageDependency CreateDependencyFromProject(Project project)
{
try
{
var projectFactory = new ProjectFactory(project);
projectFactory.Build = Build;
projectFactory.ProjectProperties = ProjectProperties;
projectFactory.BuildProject();
var builder = new PackageBuilder();
try
{
AssemblyMetadataExtractor.ExtractMetadata(builder, projectFactory.TargetPath);
}
catch
{
projectFactory.ExtractMetadataFromProject(builder);
}
projectFactory.InitializeProperties(builder);
projectFactory.ProcessNuspec(builder, null);
return new PackageDependency(
builder.Id,
VersionUtility.ParseVersionSpec(builder.Version.ToString()));
}
catch (Exception ex)
{
var message = string.Format(
CultureInfo.InvariantCulture,
LocalizedResourceManager.GetString("Error_ProcessingNuspecFile"),
project.FullPath,
ex.Message);
throw new CommandLineException(message, ex);
}
}
private void AddOutputFiles(PackageBuilder builder)
{
// Get the target framework of the project
FrameworkName targetFramework = TargetFramework;
// Get the target file path
string targetPath = TargetPath;
// List of extensions to allow in the output path
var allowedOutputExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
".dll",
".exe",
".xml",
".winmd"
};
if (IncludeSymbols)
{
// Include pdbs for symbol packages
allowedOutputExtensions.Add(".pdb");
}
string projectOutputDirectory = Path.GetDirectoryName(targetPath);
string targetFileName = Path.GetFileNameWithoutExtension(targetPath);
// By default we add all files in the project's output directory
foreach (var file in GetFiles(projectOutputDirectory, targetFileName, allowedOutputExtensions))
{
{
string extension = Path.GetExtension(file);
// Only look at files we care about
if (!allowedOutputExtensions.Contains(extension))
{
continue;
}
string targetFolder;
if (IsTool)
{
targetFolder = ToolsFolder;
}
else
{
if (targetFramework == null)
{
targetFolder = ReferenceFolder;
}
else
{
targetFolder = Path.Combine(ReferenceFolder, VersionUtility.GetShortFrameworkName(targetFramework));
}
}
var packageFile = new PhysicalPackageFile
{
SourcePath = file,
TargetPath = Path.Combine(targetFolder, Path.GetFileName(file))
};
AddFileToBuilder(builder, packageFile);
}
}
}
private void ProcessDependencies(PackageBuilder builder)
{
// get all packages and dependencies, including the ones in project references
var packagesAndDependencies = new Dictionary<String, Tuple<IPackage, PackageDependency>>();
ApplyAction(p => p.AddDependencies(packagesAndDependencies));
// list of all dependency packages
var packages = packagesAndDependencies.Values.Select(t => t.Item1).ToList();
// Add the transform file to the package builder
ProcessTransformFiles(builder, packages.SelectMany(GetTransformFiles));
var dependencies = builder.GetCompatiblePackageDependencies(targetFramework: null)
.ToDictionary(d => d.Id, StringComparer.OrdinalIgnoreCase);
// Reduce the set of packages we want to include as dependencies to the minimal set.
// Normally, packages.config has the full closure included, we only add top level
// packages, i.e. packages with in-degree 0
foreach (var package in GetMinimumSet(packages))
{
// Don't add duplicate dependencies
if (dependencies.ContainsKey(package.Id))
{
continue;
}
var dependency = packagesAndDependencies[package.Id].Item2;
dependencies[dependency.Id] = dependency;
}
if (IncludeReferencedProjects)
{
AddProjectReferenceDependencies(dependencies);
}
// TO FIX: when we persist the target framework into packages.config file,
// we need to pull that info into building the PackageDependencySet object
builder.DependencySets.Clear();
builder.DependencySets.Add(new PackageDependencySet(null, dependencies.Values));
}
private void AddDependencies(Dictionary<String,Tuple<IPackage,PackageDependency>> packagesAndDependencies)
{
var file = PackageReferenceFile.CreateFromProject(_project.FullPath);
if (!File.Exists(file.FullPath))
{
return;
}
Logger.Log(MessageLevel.Info, LocalizedResourceManager.GetString("UsingPackagesConfigForDependencies"));
// Get the solution repository
IPackageRepository repository = GetPackagesRepository();
// Collect all packages
IDictionary<PackageName, PackageReference> packageReferences =
file.GetPackageReferences()
.Where(r => !r.IsDevelopmentDependency)
.ToDictionary(r => new PackageName(r.Id, r.Version));
// add all packages and create an associated dependency to the dictionary
foreach (PackageReference reference in packageReferences.Values)
{
if (repository != null)
{
IPackage package = repository.FindPackage(reference.Id, reference.Version);
if (package != null && !packagesAndDependencies.ContainsKey(package.Id))
{
IVersionSpec spec = GetVersionConstraint(packageReferences, package);
var dependency = new PackageDependency(package.Id, spec);
packagesAndDependencies.Add(package.Id, new Tuple<IPackage,PackageDependency>(package, dependency));
}
}
}
}
private static IVersionSpec GetVersionConstraint(IDictionary<PackageName, PackageReference> packageReferences, IPackage package)
{
IVersionSpec defaultVersionConstraint = VersionUtility.ParseVersionSpec(package.Version.ToString());
PackageReference packageReference;
var key = new PackageName(package.Id, package.Version);
if (!packageReferences.TryGetValue(key, out packageReference))
{
return defaultVersionConstraint;
}
return packageReference.VersionConstraint ?? defaultVersionConstraint;
}
private IEnumerable<IPackage> GetMinimumSet(List<IPackage> packages)
{
return new Walker(packages, TargetFramework).GetMinimalSet();
}
private static void ProcessTransformFiles(PackageBuilder builder, IEnumerable<IPackageFile> transformFiles)
{
// Group transform by target file
var transformGroups = transformFiles.GroupBy(file => RemoveExtension(file.Path), StringComparer.OrdinalIgnoreCase);
var fileLookup = builder.Files.ToDictionary(file => file.Path, StringComparer.OrdinalIgnoreCase);
foreach (var tranfromGroup in transformGroups)
{
IPackageFile file;
if (fileLookup.TryGetValue(tranfromGroup.Key, out file))
{
// Replace the original file with a file that removes the transforms
builder.Files.Remove(file);
builder.Files.Add(new ReverseTransformFormFile(file, tranfromGroup));
}
}
}
/// <summary>
/// Removes a file extension keeping the full path intact
/// </summary>
private static string RemoveExtension(string path)
{
return Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
}
private IEnumerable<IPackageFile> GetTransformFiles(IPackage package)
{
return package.GetContentFiles().Where(IsTransformFile);
}
private static bool IsTransformFile(IPackageFile file)
{
return Path.GetExtension(file.Path).Equals(TransformFileExtension, StringComparison.OrdinalIgnoreCase);
}
private void AddSolutionDir()
{
// Add the solution dir to the list of properties
string solutionDir = GetSolutionDir();
// Add a path separator for Visual Studio macro compatibility
solutionDir += Path.DirectorySeparatorChar;
if (!String.IsNullOrEmpty(solutionDir))
{
if (this.ProjectProperties.ContainsKey("SolutionDir"))
{
this.Logger.Log(MessageLevel.Warning, LocalizedResourceManager.GetString("Warning_DuplicatePropertyKey"), "SolutionDir");
}
ProjectProperties["SolutionDir"] = solutionDir;
}
}
private string GetSolutionDir()
{
return ProjectHelper.GetSolutionDir(_project.DirectoryPath);
}
private IPackageRepository GetPackagesRepository()
{
string solutionDir = GetSolutionDir();
string defaultValue = DefaultSettings.GetRepositoryPath();
string target = null;
if (!String.IsNullOrEmpty(solutionDir))
{
string configValue = GetPackagesPath(solutionDir);
// solution dir exists, no default packages folder specified anywhere,
// default to hardcoded "packages" folder under solution
if (string.IsNullOrEmpty(configValue) && string.IsNullOrEmpty(defaultValue))
{
configValue = PackagesFolder;
}
if (!string.IsNullOrEmpty(configValue))
{
target = Path.Combine(solutionDir, configValue);
}
}
if (string.IsNullOrEmpty(target))
{
target = defaultValue;
}
if (!string.IsNullOrEmpty(target) && Directory.Exists(target))
{
return new SharedPackageRepository(target);
}
return null;
}
private static string GetPackagesPath(string dir)
{
string configPath = Path.Combine(dir, NuGetConfig);
try
{
// Support the hidden feature
if (File.Exists(configPath))
{
using (Stream stream = File.OpenRead(configPath))
{
// It's possible for the repositoryPath element to be missing in older versions of
// a NuGet.config file.
var repositoryPathElement = XmlUtility.LoadSafe(stream).Root.Element("repositoryPath");
if (repositoryPathElement != null)
{
return repositoryPathElement.Value;
}
}
}
}
catch (FileNotFoundException)
{
}
return null;
}
private Manifest ProcessNuspec(PackageBuilder builder, string basePath)
{
string nuspecFile = GetNuspec();
if (String.IsNullOrEmpty(nuspecFile))
{
return null;
}
Logger.Log(MessageLevel.Info, LocalizedResourceManager.GetString("UsingNuspecForMetadata"), Path.GetFileName(nuspecFile));
using (Stream stream = File.OpenRead(nuspecFile))
{
// Don't validate the manifest since this might be a partial manifest
// The bulk of the metadata might be coming from the project.
Manifest manifest = Manifest.ReadFrom(stream, this, validateSchema: true);
builder.Populate(manifest.Metadata);
if (manifest.Files != null)
{
basePath = String.IsNullOrEmpty(basePath) ? Path.GetDirectoryName(nuspecFile) : basePath;
builder.PopulateFiles(basePath, manifest.Files);
}
return manifest;
}
}
private string GetNuspec()
{
return GetNuspecPaths().FirstOrDefault(File.Exists);
}
private IEnumerable<string> GetNuspecPaths()
{
// Check for a nuspec in the project file
yield return GetContentOrNone(file => Path.GetExtension(file).Equals(Constants.ManifestExtension, StringComparison.OrdinalIgnoreCase));
// Check for a nuspec named after the project
yield return Path.Combine(_project.DirectoryPath, Path.GetFileNameWithoutExtension(_project.FullPath) + Constants.ManifestExtension);
}
private string GetContentOrNone(Func<string, bool> matcher)
{
return GetFiles("Content").Concat(GetFiles("None")).FirstOrDefault(matcher);
}
private IEnumerable<string> GetFiles(string itemType)
{
return _project.GetItems(itemType).Select(item => item.GetMetadataValue("FullPath"));
}
private void AddFiles(PackageBuilder builder, string itemType, string targetFolder)
{
// Skip files that are added by dependency packages
var references = PackageReferenceFile.CreateFromProject(_project.FullPath).GetPackageReferences();
IPackageRepository repository = GetPackagesRepository();
string projectName = Path.GetFileNameWithoutExtension(_project.FullPath);
var contentFilesInDependencies = new List<IPackageFile>();
if (references.Any() && repository != null)
{
contentFilesInDependencies = references.Select(reference => repository.FindPackage(reference.Id, reference.Version))
.Where(a => a != null)
.SelectMany(a => a.GetContentFiles())
.ToList();
}
// Get the content files from the project
foreach (var item in _project.GetItems(itemType))
{
string fullPath = item.GetMetadataValue("FullPath");
if (_excludeFiles.Contains(Path.GetFileName(fullPath)))
{
continue;
}
string targetFilePath = GetTargetPath(item);
if (!File.Exists(fullPath))
{
Logger.Log(MessageLevel.Warning, LocalizedResourceManager.GetString("Warning_FileDoesNotExist"), targetFilePath);
continue;
}
// Skip target file paths containing msbuild variables since we do not offer a way to install files with variable paths.
// These are show up in shared files found in universal apps.
if (targetFilePath.IndexOf("$(MSBuild", StringComparison.OrdinalIgnoreCase) > -1)
{
Logger.Log(MessageLevel.Warning, LocalizedResourceManager.GetString("Warning_UnresolvedFilePath"), targetFilePath);
continue;
}
// if IncludeReferencedProjects is true and we are adding source files,
// add projectName as part of the target to avoid file conflicts.
string targetPath = IncludeReferencedProjects && itemType == SourcesItemType ?
Path.Combine(targetFolder, projectName, targetFilePath) :
Path.Combine(targetFolder, targetFilePath);
// Check that file is added by dependency
IPackageFile targetFile = contentFilesInDependencies.Find(a => a.Path.Equals(targetPath, StringComparison.OrdinalIgnoreCase));
if (targetFile != null)
{
// Compare contents as well
var isEqual = ContentEquals(targetFile, fullPath);
if (isEqual)
{
Logger.Log(MessageLevel.Info, LocalizedResourceManager.GetString("PackageCommandFileFromDependencyIsNotChanged"), targetFilePath);
continue;
}
Logger.Log(MessageLevel.Info, LocalizedResourceManager.GetString("PackageCommandFileFromDependencyIsChanged"), targetFilePath);
}
var packageFile = new PhysicalPackageFile
{
SourcePath = fullPath,
TargetPath = targetPath
};
AddFileToBuilder(builder, packageFile);
}
}
private void AddFileToBuilder(PackageBuilder builder, PhysicalPackageFile packageFile)
{
if (!builder.Files.Any(p => packageFile.Path.Equals(p.Path, StringComparison.OrdinalIgnoreCase)))
{
WriteDetail(LocalizedResourceManager.GetString("AddFileToPackage"), packageFile.SourcePath, packageFile.TargetPath);
builder.Files.Add(packageFile);
}
else
{
_logger.Log(
MessageLevel.Warning,
LocalizedResourceManager.GetString("FileNotAddedToPackage"),
packageFile.SourcePath,
packageFile.TargetPath);
}
}
private void WriteDetail(string format, params object[] args)
{
var console = _logger as NuGet.Common.Console;
if (console != null && console.Verbosity == Verbosity.Detailed)
{
console.WriteLine(format, args);
}
}
internal static bool ContentEquals(IPackageFile targetFile, string fullPath)
{
bool isEqual;
using (var dependencyFileStream = targetFile.GetStream())
using (var fileContentStream = File.OpenRead(fullPath))
{
isEqual = dependencyFileStream.ContentEquals(fileContentStream);
}
return isEqual;
}
private string GetTargetPath(ProjectItem item)
{
string path = item.UnevaluatedInclude;
if (item.HasMetadata("Link"))
{
path = item.GetMetadataValue("Link");
}
return Normalize(path);
}
private string Normalize(string path)
{
string projectDirectoryPath = PathUtility.EnsureTrailingSlash(_project.DirectoryPath);
string fullPath = PathUtility.GetAbsolutePath(projectDirectoryPath, path);
// If the file is under the project root then remove the project root
if (fullPath.StartsWith(projectDirectoryPath, StringComparison.OrdinalIgnoreCase))
{
return fullPath.Substring(_project.DirectoryPath.Length).TrimStart(Path.DirectorySeparatorChar);
}
// Otherwise the file is probably a shortcut so just take the file name
return Path.GetFileName(fullPath);
}
private class Walker : PackageWalker
{
private readonly IPackageRepository _repository;
private readonly List<IPackage> _packages;
public Walker(List<IPackage> packages, FrameworkName targetFramework) :
base(targetFramework)
{
_packages = packages;
_repository = new ReadOnlyPackageRepository(packages.ToList());
}
protected override bool SkipDependencyResolveError
{
get
{
// For the pack command, when don't need to throw if a dependency is missing
// from a nuspec file.
return true;
}
}
protected override IPackage ResolveDependency(PackageDependency dependency)
{