DscCommand.cpp (3581B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "DscCommand.h" 5 #include "DscPackageResource.h" 6 #include "DscUserSettingsFileResource.h" 7 #include "DscSourceResource.h" 8 #include "DscAdminSettingsResource.h" 9 10 #ifndef AICLI_DISABLE_TEST_HOOKS 11 #include "DscTestFileResource.h" 12 #include "DscTestJsonResource.h" 13 #endif 14 15 namespace AppInstaller::CLI 16 { 17 namespace 18 { 19 Argument GetOutputFileArgument() 20 { 21 return { Execution::Args::Type::OutputFile, Resource::String::OutputDirectoryArgumentDescription, ArgumentType::Standard }; 22 } 23 } 24 25 DscCommand::DscCommand(std::string_view parent) : 26 Command(StaticName(), parent) 27 { 28 } 29 30 std::vector<std::unique_ptr<Command>> DscCommand::GetCommands() const 31 { 32 // These should all derive from DscCommandBase 33 return InitializeFromMoveOnly<std::vector<std::unique_ptr<Command>>>({ 34 std::make_unique<DscPackageResource>(FullName()), 35 std::make_unique<DscSourceResource>(FullName()), 36 std::make_unique<DscUserSettingsFileResource>(FullName()), 37 std::make_unique<DscAdminSettingsResource>(FullName()), 38 #ifndef AICLI_DISABLE_TEST_HOOKS 39 std::make_unique<DscTestFileResource>(FullName()), 40 std::make_unique<DscTestJsonResource>(FullName()), 41 #endif 42 }); 43 } 44 45 std::vector<Argument> DscCommand::GetArguments() const 46 { 47 std::vector<Argument> result; 48 49 result.emplace_back(Execution::Args::Type::DscResourceFunctionManifest, Resource::String::DscResourceFunctionDescriptionManifest, ArgumentType::Flag); 50 result.emplace_back(GetOutputFileArgument()); 51 52 return result; 53 } 54 55 Resource::LocString DscCommand::ShortDescription() const 56 { 57 return { Resource::String::DscCommandShortDescription }; 58 } 59 60 Resource::LocString DscCommand::LongDescription() const 61 { 62 return { Resource::String::DscCommandLongDescription }; 63 } 64 65 Utility::LocIndView DscCommand::HelpLink() const 66 { 67 return "https://aka.ms/winget-dsc-resources"_liv; 68 } 69 70 void DscCommand::ExecuteInternal(Execution::Context& context) const 71 { 72 if (context.Args.Contains(Execution::Args::Type::DscResourceFunctionManifest)) 73 { 74 std::filesystem::path outputDirectory{ Utility::ConvertToUTF16(context.Args.GetArg(Execution::Args::Type::OutputFile)) }; 75 std::filesystem::create_directories(outputDirectory); 76 77 std::string filePrefix = Utility::ToLower(DscCommandBase::ModuleName()); 78 79 for (const auto& command : GetCommands()) 80 { 81 DscCommandBase* commandBase = static_cast<DscCommandBase*>(command.get()); 82 83 std::filesystem::path outputPath = outputDirectory; 84 outputPath /= std::string{ filePrefix }.append(".").append(commandBase->Name()).append(".dsc.resource.json"); 85 commandBase->WriteManifest(context, outputPath); 86 } 87 } 88 else 89 { 90 OutputHelp(context.Reporter); 91 } 92 } 93 94 void DscCommand::ValidateArgumentsInternal(Execution::Args& args) const 95 { 96 if (args.Contains(Execution::Args::Type::DscResourceFunctionManifest) && 97 !args.Contains(Execution::Args::Type::OutputFile)) 98 { 99 throw CommandException(Resource::String::RequiredArgError(GetOutputFileArgument().Name())); 100 } 101 } 102 }