| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2018 The Meson development team |
| 3 |
|
| 4 |
# This file contains the detection logic for external dependencies that |
| 5 |
# are UI-related. |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import json |
| 9 |
import os |
| 10 |
import typing as T |
| 11 |
|
| 12 |
|
| 13 |
from . import ExtensionModule, ModuleInfo |
| 14 |
from .. import mlog |
| 15 |
from ..build import InvalidArguments |
| 16 |
from ..dependencies import Dependency |
| 17 |
from ..dependencies.dub import DubDependency |
| 18 |
from ..interpreterbase import typed_pos_args |
| 19 |
from ..mesonlib import Popen_safe, MesonException, listify |
| 20 |
|
| 21 |
if T.TYPE_CHECKING: |
| 22 |
from typing_extensions import Literal, TypeAlias |
| 23 |
|
| 24 |
from . import ModuleState |
| 25 |
from ..interpreter.interpreter import Interpreter |
| 26 |
from ..interpreterbase.baseobjects import TYPE_kwargs |
| 27 |
from ..programs import Program |
| 28 |
|
| 29 |
_JSONTypes: TypeAlias = T.Union[str, int, bool, None, T.List['_JSONTypes'], T.Dict[str, '_JSONTypes']] |
| 30 |
|
| 31 |
|
| 32 |
class DlangModule(ExtensionModule): |
| 33 |
class_dubbin: T.Union[Program, Literal[False], None] = None |
| 34 |
init_dub = False |
| 35 |
|
| 36 |
dubbin: T.Union[Program, Literal[False], None] |
| 37 |
|
| 38 |
INFO = ModuleInfo('dlang', '0.48.0') |
| 39 |
|
| 40 |
def __init__(self, interpreter: Interpreter): |
| 41 |
super().__init__(interpreter) |
| 42 |
self.methods.update({ |
| 43 |
'generate_dub_file': self.generate_dub_file, |
| 44 |
}) |
| 45 |
|
| 46 |
def _init_dub(self, state: ModuleState) -> None: |
| 47 |
if DlangModule.class_dubbin is None and DubDependency.class_dubbin is not None: |
| 48 |
self.dubbin = DubDependency.class_dubbin[0] |
| 49 |
DlangModule.class_dubbin = self.dubbin |
| 50 |
else: |
| 51 |
self.dubbin = DlangModule.class_dubbin |
| 52 |
|
| 53 |
if DlangModule.class_dubbin is None: |
| 54 |
self.dubbin = self.check_dub(state) |
| 55 |
DlangModule.class_dubbin = self.dubbin |
| 56 |
else: |
| 57 |
self.dubbin = DlangModule.class_dubbin |
| 58 |
|
| 59 |
if not self.dubbin: |
| 60 |
if not self.dubbin: |
| 61 |
raise MesonException('DUB not found.') |
| 62 |
|
| 63 |
@typed_pos_args('dlang.generate_dub_file', str, str) |
| 64 |
def generate_dub_file(self, state: ModuleState, args: T.Tuple[str, str], kwargs: TYPE_kwargs) -> None: |
| 65 |
if not DlangModule.init_dub: |
| 66 |
self._init_dub(state) |
| 67 |
|
| 68 |
config: T.Dict[str, _JSONTypes] = { |
| 69 |
'name': args[0] |
| 70 |
} |
| 71 |
|
| 72 |
config_path = os.path.join(args[1], 'dub.json') |
| 73 |
if os.path.exists(config_path): |
| 74 |
with open(config_path, encoding='utf-8') as ofile: |
| 75 |
try: |
| 76 |
config = json.load(ofile) |
| 77 |
except ValueError: |
| 78 |
mlog.warning('Failed to load the data in dub.json') |
| 79 |
|
| 80 |
warn_publishing = ['description', 'license'] |
| 81 |
for arg in warn_publishing: |
| 82 |
if arg not in kwargs and \ |
| 83 |
arg not in config: |
| 84 |
mlog.warning('Without', mlog.bold(arg), 'the DUB package can\'t be published') |
| 85 |
|
| 86 |
for key, value in kwargs.items(): |
| 87 |
if key == 'dependencies': |
| 88 |
values = listify(value, flatten=False) |
| 89 |
data: T.Dict[str, _JSONTypes] = {} |
| 90 |
for dep in values: |
| 91 |
if isinstance(dep, Dependency): |
| 92 |
name = dep.get_name() |
| 93 |
ret, res = self._call_dubbin(['describe', name]) |
| 94 |
if ret == 0: |
| 95 |
version = dep.get_version() |
| 96 |
if version is None: |
| 97 |
data[name] = '' |
| 98 |
else: |
| 99 |
data[name] = version |
| 100 |
config[key] = data |
| 101 |
else: |
| 102 |
def _do_validate(v: object) -> _JSONTypes: |
| 103 |
if not isinstance(v, (str, int, bool, list, dict)): |
| 104 |
raise InvalidArguments('keyword arguments must be strings, numbers, booleans, arrays, or dictionaries of such') |
| 105 |
if isinstance(v, list): |
| 106 |
for e in v: |
| 107 |
_do_validate(e) |
| 108 |
if isinstance(v, dict): |
| 109 |
for e in v.values(): |
| 110 |
_do_validate(e) |
| 111 |
return T.cast('_JSONTypes', v) |
| 112 |
|
| 113 |
config[key] = _do_validate(value) |
| 114 |
|
| 115 |
with open(config_path, 'w', encoding='utf-8') as ofile: |
| 116 |
ofile.write(json.dumps(config, indent=4, ensure_ascii=False)) |
| 117 |
|
| 118 |
def _call_dubbin(self, args: T.List[str], env: T.Optional[T.Mapping[str, str]] = None) -> T.Tuple[int, str]: |
| 119 |
assert self.dubbin is not None and self.dubbin is not False, 'for mypy' |
| 120 |
p, out = Popen_safe(self.dubbin.get_command() + args, env=env)[0:2] |
| 121 |
return p.returncode, out.strip() |
| 122 |
|
| 123 |
def check_dub(self, state: ModuleState) -> T.Union[Program, Literal[False]]: |
| 124 |
dubbin = state.find_program('dub', silent=True) |
| 125 |
if dubbin.found(): |
| 126 |
try: |
| 127 |
p, out = Popen_safe(dubbin.get_command() + ['--version'])[0:2] |
| 128 |
if p.returncode != 0: |
| 129 |
mlog.warning('Found dub {!r} but couldn\'t run it' |
| 130 |
''.format(' '.join(dubbin.get_command()))) |
| 131 |
# Set to False instead of None to signify that we've already |
| 132 |
# searched for it and not found it |
| 133 |
else: |
| 134 |
mlog.log('Found DUB:', mlog.green('YES'), ':', mlog.bold(dubbin.get_path() or ''), |
| 135 |
'({})'.format(out.strip())) |
| 136 |
return dubbin |
| 137 |
except (FileNotFoundError, PermissionError): |
| 138 |
pass |
| 139 |
mlog.log('Found DUB:', mlog.red('NO')) |
| 140 |
return False |
| 141 |
|
| 142 |
def initialize(interp: Interpreter) -> DlangModule: |
| 143 |
return DlangModule(interp) |