Skip to content

Commit e4d4c4c

Browse files
Tim Sneathdanielroek
authored andcommitted
[path_provider] Add Windows support (flutter#2818)
Implements path_provider for Windows, adding path_provider_windows and endorsing it in path_provider.
1 parent 4629878 commit e4d4c4c

20 files changed

+946
-41
lines changed

packages/path_provider/path_provider/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.6.15
2+
3+
* Endorse Windows implementation.
4+
* Remove the need to call disablePathProviderPlatformOverride in tests
5+
16
## 1.6.14
27

38
* Update package:e2e -> package:integration_test

packages/path_provider/path_provider/README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ Please see the example app of this plugin for a full example.
2323

2424
### Usage in tests
2525

26-
`path_provider` now uses a `PlatformInterface`, meaning that not all platforms share the a single `PlatformChannel`-based implementation.
26+
`path_provider` now uses a `PlatformInterface`, meaning that not all platforms share the a single `PlatformChannel`-based implementation.
2727
With that change, tests should be updated to mock `PathProviderPlatform` rather than `PlatformChannel`.
2828

2929
See this `path_provider` [test](https://github.com/flutter/plugins/blob/master/packages/path_provider/path_provider/test/path_provider_test.dart) for an example.
30-
31-
You will also have to temporarily add the following line to the setup of your test.
32-
```dart
33-
disablePathProviderPlatformOverride = true;
34-
```
35-
36-
See this [issue](https://github.com/flutter/flutter/issues/52267), for more details on why this is needed.

packages/path_provider/path_provider/lib/path_provider.dart

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,37 @@ import 'dart:io' show Directory, Platform;
77

88
import 'package:flutter/foundation.dart' show kIsWeb, visibleForTesting;
99
import 'package:path_provider_linux/path_provider_linux.dart';
10+
import 'package:path_provider_windows/path_provider_windows.dart';
1011
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
12+
import 'package:path_provider_platform_interface/src/method_channel_path_provider.dart';
1113

1214
export 'package:path_provider_platform_interface/path_provider_platform_interface.dart'
1315
show StorageDirectory;
1416

15-
/// Disables platform override in order to use a manually registered [PathProviderPlatform], only for testing right now
16-
///
17-
/// Make sure to disable the override before using any of the `path_provider` methods
18-
/// To use your own [PathProviderPlatform], make sure to include the following lines
19-
/// ```
20-
/// PathProviderPlatform.instance = YourPathProviderPlatform();
21-
/// disablePathProviderPlatformOverride = true;
22-
/// // Use the `path_provider` methods:
23-
/// final dir = await getTemporaryDirectory();
24-
/// ```
25-
/// See this issue https://github.com/flutter/flutter/issues/52267 for why this is required
2617
@visibleForTesting
27-
set disablePathProviderPlatformOverride(bool override) {
28-
_disablePlatformOverride = override;
29-
}
18+
@Deprecated('This is no longer necessary, and is now a no-op')
19+
set disablePathProviderPlatformOverride(bool override) {}
3020

31-
bool _disablePlatformOverride = false;
32-
PathProviderPlatform __platform;
21+
bool _manualDartRegistrationNeeded = true;
3322

34-
// This is to manually endorse the linux path provider until automatic registration of dart plugins is implemented.
35-
// See this issue https://github.com/flutter/flutter/issues/52267 for details
3623
PathProviderPlatform get _platform {
37-
if (__platform != null) {
38-
return __platform;
39-
}
40-
if (!kIsWeb && Platform.isLinux && !_disablePlatformOverride) {
41-
__platform = PathProviderLinux();
42-
} else {
43-
__platform = PathProviderPlatform.instance;
24+
// This is to manually endorse Dart implementations until automatic
25+
// registration of Dart plugins is implemented. For details see
26+
// https://github.com/flutter/flutter/issues/52267.
27+
if (_manualDartRegistrationNeeded) {
28+
// Only do the initial registration if it hasn't already been overridden
29+
// with a non-default instance.
30+
if (!kIsWeb && PathProviderPlatform.instance is MethodChannelPathProvider) {
31+
if (Platform.isLinux) {
32+
PathProviderPlatform.instance = PathProviderLinux();
33+
} else if (Platform.isWindows) {
34+
PathProviderPlatform.instance = PathProviderWindows();
35+
}
36+
}
37+
_manualDartRegistrationNeeded = false;
4438
}
45-
return __platform;
39+
40+
return PathProviderPlatform.instance;
4641
}
4742

4843
/// Path to the temporary directory on the device that is not backed up and is

packages/path_provider/path_provider/pubspec.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: path_provider
2-
description: Flutter plugin for getting commonly used locations on the Android &
3-
iOS file systems, such as the temp and app data directories.
2+
description: Flutter plugin for getting commonly used locations on host platform file systems, such as the temp and app data directories.
43
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider
5-
version: 1.6.14
4+
version: 1.6.15
65

76
flutter:
87
plugin:
@@ -16,13 +15,16 @@ flutter:
1615
default_package: path_provider_macos
1716
linux:
1817
default_package: path_provider_linux
18+
windows:
19+
default_package: path_provider_windows
1920

2021
dependencies:
2122
flutter:
2223
sdk: flutter
2324
path_provider_platform_interface: ^1.0.1
2425
path_provider_macos: ^0.0.4
2526
path_provider_linux: ^0.0.1
27+
path_provider_windows: ^0.0.1
2628

2729
dev_dependencies:
2830
integration_test:

packages/path_provider/path_provider/test/path_provider_test.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ void main() {
2525

2626
setUp(() async {
2727
PathProviderPlatform.instance = MockPathProviderPlatform();
28-
// This is required because we manually register the Linux path provider when on the Linux platform.
29-
// Will be removed when automatic registration of dart plugins is implemented.
30-
// See this issue https://github.com/flutter/flutter/issues/52267 for details
31-
disablePathProviderPlatformOverride = true;
3228
});
3329

3430
test('getTemporaryDirectory', () async {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.packages
2+
.flutter-plugins
3+
pubspec.lock
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 0.0.2
2+
3+
* README update for endorsement.
4+
* Changed getApplicationSupportPath location.
5+
* Removed getLibraryPath.
6+
7+
## 0.0.1+2
8+
9+
* The initial implementation of path_provider for Windows
10+
* Implements getTemporaryPath, getApplicationSupportPath, getLibraryPath,
11+
getApplicationDocumentsPath and getDownloadsPath.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright 2017 The Chromium Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification,
4+
are permitted provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# path_provider_windows
2+
3+
The Windows implementation of [`path_provider`][1].
4+
5+
**Please set your constraint to `path_provider_windows: '>=0.0.y+x <2.0.0'`**
6+
7+
## Backward compatible 1.0.0 version is coming
8+
9+
The plugin has reached a stable API, we guarantee that version `1.0.0` will be backward compatible with `0.0.y+z`.
10+
Please use `path_provider_windows: '>=0.0.y+x <2.0.0'` as your dependency constraint to allow a smoother ecosystem migration.
11+
For more details see: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
12+
13+
## Usage
14+
15+
### Import the package
16+
17+
This package has been endorsed, meaning that you only need to add `path_provider`
18+
as a dependency in your `pubspec.yaml`. It will be automatically included in your app
19+
when you depend on `package:path_provider`.
20+
21+
This is what the above means to your `pubspec.yaml`:
22+
23+
```yaml
24+
...
25+
dependencies:
26+
...
27+
path_provider: ^1.6.15
28+
...
29+
```
30+
31+
[1]:../
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
**/ios/Flutter/.last_build_id
26+
.dart_tool/
27+
.flutter-plugins
28+
.flutter-plugins-dependencies
29+
.packages
30+
.pub-cache/
31+
.pub/
32+
/build/
33+
34+
# Web related
35+
lib/generated_plugin_registrant.dart
36+
37+
# Symbolication related
38+
app.*.symbols
39+
40+
# Obfuscation related
41+
app.*.map.json
42+
43+
# Exceptions to above rules.
44+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: f2320c3b7a42bc27e7f038212eed1b01f4269641
8+
channel: master
9+
10+
project_type: app
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# path_provider_windows_example
2+
3+
Demonstrates how to use the path_provider_windows plugin.
4+
5+
## Getting Started
6+
7+
For help getting started with Flutter, view our online
8+
[documentation](http://flutter.io/).
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2020 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: public_member_api_docs
6+
7+
import 'dart:async';
8+
9+
import 'package:flutter/material.dart';
10+
import 'package:path_provider_windows/path_provider_windows.dart';
11+
12+
void main() async {
13+
runApp(MyApp());
14+
}
15+
16+
/// Sample app
17+
class MyApp extends StatefulWidget {
18+
@override
19+
_MyAppState createState() => _MyAppState();
20+
}
21+
22+
class _MyAppState extends State<MyApp> {
23+
String _tempDirectory = 'Unknown';
24+
String _downloadsDirectory = 'Unknown';
25+
String _appSupportDirectory = 'Unknown';
26+
String _documentsDirectory = 'Unknown';
27+
28+
@override
29+
void initState() {
30+
super.initState();
31+
initDirectories();
32+
}
33+
34+
// Platform messages are asynchronous, so we initialize in an async method.
35+
Future<void> initDirectories() async {
36+
String tempDirectory;
37+
String downloadsDirectory;
38+
String appSupportDirectory;
39+
String documentsDirectory;
40+
final PathProviderWindows provider = PathProviderWindows();
41+
42+
try {
43+
tempDirectory = await provider.getTemporaryPath();
44+
} catch (exception) {
45+
tempDirectory = 'Failed to get temp directory: $exception';
46+
}
47+
try {
48+
downloadsDirectory = await provider.getDownloadsPath();
49+
} catch (exception) {
50+
downloadsDirectory = 'Failed to get downloads directory: $exception';
51+
}
52+
53+
try {
54+
documentsDirectory = await provider.getApplicationDocumentsPath();
55+
} catch (exception) {
56+
documentsDirectory = 'Failed to get documents directory: $exception';
57+
}
58+
59+
try {
60+
appSupportDirectory = await provider.getApplicationSupportPath();
61+
} catch (exception) {
62+
appSupportDirectory = 'Failed to get app support directory: $exception';
63+
}
64+
65+
setState(() {
66+
_tempDirectory = tempDirectory;
67+
_downloadsDirectory = downloadsDirectory;
68+
_appSupportDirectory = appSupportDirectory;
69+
_documentsDirectory = documentsDirectory;
70+
});
71+
}
72+
73+
@override
74+
Widget build(BuildContext context) {
75+
return MaterialApp(
76+
home: Scaffold(
77+
appBar: AppBar(
78+
title: const Text('Path Provider example app'),
79+
),
80+
body: Center(
81+
child: Column(
82+
children: [
83+
Text('Temp Directory: $_tempDirectory\n'),
84+
Text('Documents Directory: $_documentsDirectory\n'),
85+
Text('Downloads Directory: $_downloadsDirectory\n'),
86+
Text('Application Support Directory: $_appSupportDirectory\n'),
87+
],
88+
),
89+
),
90+
),
91+
);
92+
}
93+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: path_provider_example
2+
description: Demonstrates how to use the path_provider plugin.
3+
4+
dependencies:
5+
flutter:
6+
sdk: flutter
7+
path_provider_windows: any
8+
9+
dependency_overrides:
10+
path_provider_windows:
11+
path: ../
12+
13+
dev_dependencies:
14+
e2e: ^0.2.1
15+
flutter_driver:
16+
sdk: flutter
17+
test: any
18+
pedantic: ^1.8.0
19+
20+
flutter:
21+
uses-material-design: true

0 commit comments

Comments
 (0)