Debian Codegraph @

azure_functions_devops_build/yaml/github_yaml_manager.py

raw
1 # --------------------------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for license information.
4 # --------------------------------------------------------------------------------------------
5
6 from os import path
7 from datetime import datetime
8 from jinja2 import Environment, FileSystemLoader, select_autoescape
9 from ..repository.github_repository_manager import GithubRepositoryManager
10 from ..base.base_github_manager import BaseGithubManager
11 from ..constants import (WINDOWS, PYTHON, NODE, DOTNET, POWERSHELL)
12 from ..exceptions import LanguageNotSupportException
13
14
15 class GithubYamlManager(BaseGithubManager):
16
17 def __init__(self, language, app_type, github_pat, github_repository):
18 super(GithubYamlManager, self).__init__(pat=github_pat)
19 self._github_repo_mgr = GithubRepositoryManager(pat=github_pat)
20 self._github_repository = github_repository
21 self._language = language
22 self._app_type = app_type
23 self.jinja_env = Environment(
24 loader=FileSystemLoader(path.join(path.abspath(path.dirname(__file__)), 'templates')),
25 autoescape=select_autoescape(['jinja'])
26 )
27
28 def create_yaml(self, overwrite=False):
29 if self._language == PYTHON:
30 language_str = 'python'
31 package_route = '$(System.DefaultWorkingDirectory)'
32 dependencies = self._python_dependencies()
33 elif self._language == NODE:
34 language_str = 'node'
35 package_route = '$(System.DefaultWorkingDirectory)'
36 dependencies = self._node_dependencies()
37 elif self._language == DOTNET:
38 language_str = 'dotnet'
39 package_route = '$(System.DefaultWorkingDirectory)/publish_output/s'
40 dependencies = self._dotnet_dependencies()
41 elif self._language == POWERSHELL:
42 language_str = 'powershell'
43 package_route = '$(System.DefaultWorkingDirectory)'
44 dependencies = self._powershell_dependencies()
45 else:
46 raise LanguageNotSupportException(self._language)
47
48 if self._app_type == WINDOWS:
49 platform_str = 'windows'
50 yaml = self._generate_yaml(dependencies, 'VS2017-Win2016', language_str, platform_str, package_route)
51 else:
52 platform_str = 'linux'
53 yaml = self._generate_yaml(dependencies, 'ubuntu-16.04', language_str, platform_str, package_route)
54
55 if overwrite:
56 return self._overwrite_yaml_file(yaml)
57 else:
58 return self._commit_yaml_file(yaml)
59
60 def _commit_yaml_file(self, data):
61 return self._github_repo_mgr.commit_file(
62 repository_fullname=self._github_repository,
63 file_path="azure-pipelines.yml",
64 file_data=data,
65 commit_message="Created azure-pipelines.yml by Azure CLI ({time})".format(
66 time=datetime.utcnow().strftime("%Y-%m-%d %X UTC")
67 ),
68 )
69
70 def _overwrite_yaml_file(self, data):
71 sha = self._github_repo_mgr.get_content(
72 self._github_repository,
73 'azure-pipelines.yml',
74 get_metadata=True
75 ).get("sha")
76 return self._github_repo_mgr.commit_file(
77 repository_fullname=self._github_repository,
78 file_path="azure-pipelines.yml",
79 file_data=data,
80 commit_message="Overwritten azure-pipelines.yml by Azure CLI ({time})".format(
81 time=datetime.utcnow().strftime("%Y-%m-%d %X UTC")
82 ),
83 sha=sha
84 )
85
86 def _generate_yaml(self, dependencies, vmImage, language_str, platform_str, package_route):
87 template = self.jinja_env.get_template('build.jinja')
88 outputText = template.render(dependencies=dependencies, vmImage=vmImage,
89 language=language_str, platform=platform_str,
90 package_route=package_route)
91 return outputText
92
93 def _requires_extensions(self):
94 return self._github_repo_mgr.check_github_file(self._github_repository, 'extensions.csproj')
95
96 def _requires_pip(self):
97 return self._github_repo_mgr.check_github_file(self._github_repository, 'requirements.txt')
98
99 def _requires_npm(self):
100 return self._github_repo_mgr.check_github_file(self._github_repository, 'package.json')
101
102 def _inject_extensions_build(self, dependencies):
103 if self._app_type == WINDOWS:
104 runtime = 'win10-x64'
105 else:
106 runtime = 'ubuntu.16.04-x64'
107
108 dependencies.append("- task: DotNetCoreCLI@2")
109 dependencies.append(" displayName: '.NET Core build extensions'")
110 dependencies.append(" inputs:")
111 dependencies.append(" projects: extensions.csproj")
112 dependencies.append(" arguments: '--runtime {runtime} --output bin/'".format(runtime=runtime))
113
114 def _python_dependencies(self):
115 """Helper to create the standard python dependencies"""
116 dependencies = []
117 if self._requires_extensions():
118 self._inject_extensions_build(dependencies)
119 dependencies.append('- task: UsePythonVersion@0')
120 dependencies.append(' displayName: "Setting python version to 3.6 as required by functions"')
121 dependencies.append(' inputs:')
122 dependencies.append(' versionSpec: \'3.6\'')
123 dependencies.append(' architecture: \'x64\'')
124 dependencies.append('- script: |')
125 dependencies.append(' python3.6 -m venv worker_venv')
126 dependencies.append(' source worker_venv/bin/activate')
127 dependencies.append(' pip3.6 install setuptools')
128 if self._requires_pip():
129 dependencies.append(' pip3.6 install -r requirements.txt')
130 return dependencies
131
132 def _node_dependencies(self):
133 """Helper to create the standard node dependencies"""
134 dependencies = []
135 if self._requires_extensions():
136 self._inject_extensions_build(dependencies)
137 if self._requires_npm():
138 dependencies.append('- script: |')
139 dependencies.append(' npm install')
140 dependencies.append(' npm run build --if-present')
141 dependencies.append(' npm prune --production')
142
143 return dependencies
144
145 def _dotnet_dependencies(self):
146 """Helper to create the standard dotnet dependencies"""
147 dependencies = []
148 dependencies.append('- script: |')
149 dependencies.append(' dotnet restore')
150 dependencies.append(' dotnet build --configuration Release')
151 dependencies.append("- task: DotNetCoreCLI@2")
152 dependencies.append(" inputs:")
153 dependencies.append(" command: publish")
154 dependencies.append(" arguments: '--configuration Release --output publish_output'")
155 dependencies.append(" projects: '*.csproj'")
156 dependencies.append(" publishWebProjects: false")
157 dependencies.append(" modifyOutputPath: true")
158 dependencies.append(" zipAfterPublish: false")
159 return dependencies
160
161 def _powershell_dependencies(self):
162 dependencies = []
163 if self._requires_extensions():
164 self._inject_extensions_build(dependencies)
165
166 return dependencies