Skip to content

Commit 6e80fb8

Browse files
author
Jouke Witteveen
committed
Add suffix function to the fs module
1 parent 12563f7 commit 6e80fb8

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

docs/markdown/Fs-module.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,26 @@ fs.name('foo/bar/baz.dll.a') # baz.dll.a
206206
*since 0.54.0*
207207

208208
Returns the last component of the path, dropping the last part of the
209-
suffix
209+
suffix.
210210

211211
```meson
212212
fs.stem('foo/bar/baz.dll') # baz
213213
fs.stem('foo/bar/baz.dll.a') # baz.dll
214214
```
215215

216+
### suffix
217+
218+
*since 1.9.0*
219+
220+
Returns the last dot-separated portion of the final component of the path
221+
including the dot, if any.
222+
223+
```meson
224+
fs.suffix('foo/bar/baz.dll') # .dll
225+
fs.suffix('foo/bar/baz.dll.a') # .a
226+
fs.suffix('foo/bar') # (empty)
227+
```
228+
216229
### read
217230
- `read(path, encoding: 'utf-8')` *(since 0.57.0)*:
218231
return a [string](Syntax.md#strings) with the contents of the given `path`.

docs/markdown/snippets/fs_suffix.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Added suffix function to the FS module
2+
3+
The basename and stem were already available. For completeness, expose also the
4+
suffix.

mesonbuild/modules/fs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self, interpreter: 'Interpreter') -> None:
6262
'replace_suffix': self.replace_suffix,
6363
'size': self.size,
6464
'stem': self.stem,
65+
'suffix': self.suffix,
6566
})
6667

6768
def _absolute_dir(self, state: 'ModuleState', arg: 'FileOrString') -> Path:
@@ -214,6 +215,13 @@ def stem(self, state: 'ModuleState', args: T.Tuple[T.Union[FileOrString, BuildTa
214215
path = self._obj_to_path('fs.stem', args[0], state)
215216
return str(path.stem)
216217

218+
@noKwargs
219+
@typed_pos_args('fs.suffix', (str, File, CustomTarget, CustomTargetIndex, BuildTarget))
220+
@FeatureNew('fs.suffix', '1.9.0')
221+
def suffix(self, state: 'ModuleState', args: T.Tuple[T.Union[FileOrString, BuildTargetTypes]], kwargs: T.Dict[str, T.Any]) -> str:
222+
path = self._obj_to_path('fs.suffix', args[0], state)
223+
return str(path.suffix)
224+
217225
@FeatureNew('fs.read', '0.57.0')
218226
@typed_pos_args('fs.read', (str, File))
219227
@typed_kwargs('fs.read', KwargInfo('encoding', str, default='utf-8'))

test cases/common/220 fs module/meson.build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ assert(fs.name(f[1]) == 'meson.build', 'failed to get basename')
153153
assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix')
154154
if host_machine.system() in ['cygwin', 'windows']
155155
assert(fs.name(btgt) == 'btgt.exe', 'failed to get basename of build target')
156+
assert(fs.suffix(btgt) == '.exe', 'failed to get build target suffix')
156157
else
157158
assert(fs.name(btgt) == 'btgt', 'failed to get basename of build target')
159+
assert(fs.suffix(btgt) == '', 'failed to get build target suffix')
158160
endif
159161
assert(fs.name(ctgt) == 'ctgt.txt', 'failed to get basename of custom target')
160162
assert(fs.name(ctgt[0]) == 'ctgt.txt', 'failed to get basename of custom target index')
@@ -163,6 +165,11 @@ assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compo
163165
assert(fs.stem(btgt) == 'btgt', 'failed to get stem of build target')
164166
assert(fs.stem(ctgt) == 'ctgt', 'failed to get stem of custom target')
165167
assert(fs.stem(ctgt[0]) == 'ctgt', 'failed to get stem of custom target index')
168+
assert(fs.suffix('foo/bar/baz') == '', 'failed to get missing suffix')
169+
assert(fs.suffix('foo/bar/baz.dll') == '.dll', 'failed to get plain suffix')
170+
assert(fs.suffix('foo/bar/baz.dll.a') == '.a', 'failed to get final suffix')
171+
assert(fs.suffix(ctgt) == '.txt', 'failed to get suffix of custom target')
172+
assert(fs.suffix(ctgt[0]) == '.txt', 'failed to get suffix of custom target index')
166173

167174
# relative_to
168175
if build_machine.system() == 'windows'

0 commit comments

Comments
 (0)