private static void CopyServiceManifest(ServiceFabricServiceProject service, DirectoryInfo basePackagePath)
        {
            var servicePackageFolder = service.GetServicePackageFolder(basePackagePath);

            if (!servicePackageFolder.Exists)
            {
                servicePackageFolder.Create();
            }

            File.Copy(service.SourceServiceManifestPath, service.GetServiceManifestTargetFile(servicePackageFolder).FullName);
        }
        private async Task PackageFiles(
            ServiceFabricApplicationProject appData,
            ServiceFabricServiceProject serviceProject,
            KeyValuePair <string, GlobalVersion> service)
        {
            var                    appPackagePath     = appData.GetPackagePath(_baseConfig.PackageOutputPath);
            var                    servicePackagePath = serviceProject.GetServicePackageFolder(appPackagePath);
            DirectoryInfo          directory;
            IEnumerable <FileInfo> files;
            var                    package = serviceProject.SubPackages
                                             .First(x => x.PackageType == service.Value.PackageType);

            var extraFiles = _packageConfig
                             .ExternalIncludes
                             .Where(x => x
                                    .ApplicationTypeName.Equals(appData.ApplicationTypeName,
                                                                StringComparison.CurrentCultureIgnoreCase))
                             .Where(x => x
                                    .ServiceManifestName.Equals(serviceProject.ServiceName,
                                                                StringComparison.CurrentCultureIgnoreCase))
                             .Where(x => x
                                    .PackageName.Equals(package.Name,
                                                        StringComparison.CurrentCultureIgnoreCase));

            if (service.Value.PackageType == PackageType.Code)
            {
                if (!serviceProject.IsGuestExecutable)
                {
                    directory = new DirectoryInfo(Path.Combine(serviceProject.ProjectFolder.FullName,
                                                               appData.BuildOutputPathSuffix));

                    files = directory
                            .GetFiles("*", SearchOption.AllDirectories)
                            .Where(x => _packageConfig.HashIncludeExtensions.Any(include =>
                                                                                 x.FullName.ToLowerInvariant().EndsWith(include.ToLowerInvariant())))
                            .Select(x => x.FullName)
                            .OrderBy(x => x)
                            .Select(x => new FileInfo(x));
                }
                else
                {
                    directory = new DirectoryInfo(Path.Combine(serviceProject.ProjectFolder.FullName, "Code"));

                    files = directory
                            .GetFiles("*", SearchOption.AllDirectories)
                            .Select(x => x.FullName)
                            .OrderBy(x => x)
                            .Select(x => new FileInfo(x));
                }
            }
            else
            {
                directory = new DirectoryInfo(Path.Combine(serviceProject.PackageRoot.FullName, package.Name));
                files     = directory
                            .GetFiles("*", SearchOption.AllDirectories)
                            .Select(x => x.FullName)
                            .OrderBy(x => x)
                            .Select(x => new FileInfo(x));
            }

            var basePathLength   = directory.FullName.Length;
            var subPackageFolder = package.GetSubPackageTargetPath(servicePackagePath);

            if (!subPackageFolder.Exists)
            {
                subPackageFolder.Create();
            }

            foreach (var file in files)
            {
                var relPath    = file.FullName.Remove(0, basePathLength + 1);
                var targetFile = new FileInfo(Path.Combine(subPackageFolder.FullName, relPath));
                if (!Directory.Exists(targetFile.DirectoryName))
                {
                    Directory.CreateDirectory(targetFile.DirectoryName);
                }

                File.Copy(file.FullName, targetFile.FullName);
            }

            foreach (var externalFile in extraFiles)
            {
                var file = await _fileHandler
                           .GetFileAsBytesAsync(externalFile.SourceFileName)
                           .ConfigureAwait(false);

                if (!file.IsSuccessful)
                {
                    throw new IOException("Failed to get external file from storage");
                }

                File.WriteAllBytes(Path.Combine(subPackageFolder.FullName, externalFile.TargetFileName), file.ResponseContent);
            }
        }