summaryrefslogtreecommitdiff
path: root/src/buildtool/common/action.hpp
blob: 4a4be18397a4d9a617bb0e52bcd4fa0627a7727b (plain)
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
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef INCLUDED_SRC_BUILDTOOL_COMMON_ACTION_HPP
#define INCLUDED_SRC_BUILDTOOL_COMMON_ACTION_HPP

#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "src/buildtool/common/identifier.hpp"

class Action {
  public:
    using LocalPath = std::string;

    Action(std::string action_id,
           std::vector<std::string> command,
           std::string cwd,
           std::map<std::string, std::string> env_vars,
           std::optional<std::string> may_fail,
           bool no_cache,
           double timeout_scale,
           std::map<std::string, std::string> execution_properties)
        : id_{std::move(action_id)},
          command_{std::move(command)},
          cwd_{std::move(cwd)},
          env_{std::move(env_vars)},
          may_fail_{std::move(may_fail)},
          no_cache_{no_cache},
          timeout_scale_{timeout_scale},
          execution_properties_{std::move(std::move(execution_properties))} {}

    Action(std::string action_id,
           std::vector<std::string> command,
           std::map<std::string, std::string> env_vars)
        : Action(std::move(action_id),
                 std::move(command),
                 "",
                 std::move(env_vars),
                 std::nullopt,
                 false,
                 1.0,
                 std::map<std::string, std::string>{}) {}

    [[nodiscard]] auto Id() const noexcept -> ActionIdentifier const& {
        return id_;
    }

    [[nodiscard]] auto Command() && noexcept -> std::vector<std::string> {
        return std::move(command_);
    }

    [[nodiscard]] auto Command() const& noexcept
        -> std::vector<std::string> const& {
        return command_;
    }

    [[nodiscard]] auto Cwd() const noexcept -> std::string const& {
        return cwd_;
    }

    [[nodiscard]] auto Env() const& noexcept
        -> std::map<std::string, std::string> const& {
        return env_;
    }

    [[nodiscard]] auto Env() && noexcept -> std::map<std::string, std::string> {
        return std::move(env_);
    }

    [[nodiscard]] auto IsTreeAction() const noexcept -> bool {
        return is_tree_;
    }
    [[nodiscard]] auto MayFail() const noexcept
        -> std::optional<std::string> const& {
        return may_fail_;
    }
    [[nodiscard]] auto NoCache() const noexcept -> bool { return no_cache_; }
    [[nodiscard]] auto TimeoutScale() const noexcept -> double {
        return timeout_scale_;
    }

    [[nodiscard]] auto ExecutionProperties() const& noexcept
        -> std::map<std::string, std::string> const& {
        return execution_properties_;
    }

    [[nodiscard]] auto ExecutionProperties() && noexcept
        -> std::map<std::string, std::string> {
        return std::move(execution_properties_);
    }

    [[nodiscard]] static auto CreateTreeAction(
        ActionIdentifier const& id) noexcept -> Action {
        return Action{id};
    }

  private:
    ActionIdentifier id_;
    std::vector<std::string> command_;
    std::string cwd_;
    std::map<std::string, std::string> env_;
    bool is_tree_{};
    std::optional<std::string> may_fail_;
    bool no_cache_{};
    double timeout_scale_{};
    std::map<std::string, std::string> execution_properties_;

    explicit Action(ActionIdentifier id) noexcept
        : id_{std::move(id)}, is_tree_{true} {}
};

#endif  // INCLUDED_SRC_BUILDTOOL_COMMON_ACTION_HPP