Skip to content

Commit 34c0119

Browse files
[cross_file_io] dart:io implementation of cross_file (flutter#12876)
Part of flutter#91869 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 2bdcd92 commit 34c0119

38 files changed

Lines changed: 2093 additions & 0 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Below is a list of people and organizations that have contributed
2+
# to the Flutter project. Names should be added to the list like so:
3+
#
4+
# Name/Organization <email address>
5+
6+
Google LLC
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
* Initial release.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright 2013 The Flutter Authors
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# cross\_file\_io
2+
3+
The `dart:io` implementation of [`cross_file`][1].
4+
5+
## Usage
6+
7+
This package is [endorsed][2], which means you can simply use `cross_file`
8+
normally. This package will be automatically included in your app when you do,
9+
so you do not need to add it to your `pubspec.yaml`.
10+
11+
However, if you `import` this package to use any of its APIs directly, you
12+
should add it to your `pubspec.yaml` as usual.
13+
14+
[1]: https://pub.dev/packages/cross_file
15+
[2]: https://flutter.dev/to/endorsed-federated-plugin
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Platform Implementation Test App
2+
3+
This is a test app for manual testing and automated integration testing
4+
of this platform implementation. It is not intended to demonstrate actual use of
5+
this package, since the intent is that plugin clients use the app-facing
6+
package.
7+
8+
Unless you are making changes to this implementation package, this example is
9+
very unlikely to be relevant.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, world!
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:cross_file_io/cross_file_io.dart';
8+
import 'package:cross_file_platform_interface/cross_file_platform_interface.dart';
9+
import 'package:flutter_test/flutter_test.dart';
10+
import 'package:integration_test/integration_test.dart';
11+
import 'package:path/path.dart' as path;
12+
import 'package:path_provider/path_provider.dart';
13+
14+
void main() {
15+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
16+
17+
testWidgets('Can read from file using dart:io implementation', (WidgetTester tester) async {
18+
final Directory dir = await getApplicationCacheDirectory();
19+
20+
final xDir = PlatformFileSystemXDirectory(PlatformFileSystemXDirectoryCreationParams(dir.path));
21+
expect(await xDir.exists(), isTrue);
22+
23+
final file = PlatformFileSystemXFile(
24+
PlatformFileSystemXFileCreationParams(path.join(dir.path, 'hello.txt')),
25+
);
26+
(file.extension! as IOFileSystemXFileExtension).file
27+
..createSync()
28+
..writeAsStringSync('Hello, World!');
29+
30+
expect(await file.readAsString(), 'Hello, World!');
31+
});
32+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:cross_file_platform_interface/cross_file_platform_interface.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:mime/mime.dart' as mime;
8+
9+
void main() {
10+
runApp(const MaterialApp(home: FileOpenScreen()));
11+
}
12+
13+
/// Example screen to open a file selector and display it.
14+
class FileOpenScreen extends StatelessWidget {
15+
/// Constructs a [FileOpenScreen].
16+
const FileOpenScreen({super.key});
17+
18+
Future<PlatformXFile?> _getTextFile() async {
19+
// Implement this method to retrieve a text file.
20+
return null;
21+
}
22+
23+
Future<PlatformXDirectory?> _getDirectory() async {
24+
// Implement this method to retrieve a directory.
25+
return null;
26+
}
27+
28+
Future<void> _openTextFile(BuildContext context) async {
29+
final PlatformXFile? file = await _getTextFile();
30+
31+
if (file != null) {
32+
final String filename = await file.name() ?? file.params.uri;
33+
34+
switch (mime.lookupMimeType(filename)) {
35+
case final String mimeType when mimeType.startsWith('text'):
36+
final String fileContents = await file.readAsString();
37+
if (context.mounted) {
38+
await showDialog<void>(
39+
context: context,
40+
builder: (BuildContext context) =>
41+
TextDisplay(filename: filename, fileContents: fileContents),
42+
);
43+
}
44+
case _:
45+
debugPrint('File Uri: ${file.params.uri}');
46+
debugPrint('Filename: $filename');
47+
debugPrint('File Length: ${await file.length()}');
48+
debugPrint('File Last Modified: ${await file.lastModified()}');
49+
return;
50+
}
51+
} else {
52+
debugPrint('No file selected.');
53+
}
54+
}
55+
56+
Future<void> _openDirectory() async {
57+
final PlatformXDirectory? directory = await _getDirectory();
58+
59+
if (directory != null) {
60+
debugPrint('Directory Uri: ${directory.params.uri}');
61+
debugPrint('Directory exists: ${await directory.exists()}');
62+
63+
debugPrint('List of Entities:');
64+
await for (final PlatformXEntity entity in directory.list(const PlatformListParams())) {
65+
switch (entity) {
66+
case final PlatformXFile file:
67+
final String filename = await file.name() ?? file.params.uri;
68+
debugPrint('\tFile: $filename');
69+
case final PlatformXDirectory directory:
70+
debugPrint('\tDirectory: ${directory.params.uri}');
71+
}
72+
}
73+
} else {
74+
debugPrint('No directory selected.');
75+
}
76+
}
77+
78+
@override
79+
Widget build(BuildContext context) {
80+
return Scaffold(
81+
appBar: AppBar(title: const Text('Open a Text File'), backgroundColor: Colors.blue),
82+
body: Center(
83+
child: Column(
84+
mainAxisAlignment: MainAxisAlignment.center,
85+
children: <Widget>[
86+
ElevatedButton(
87+
style: ElevatedButton.styleFrom(
88+
foregroundColor: Colors.blue,
89+
backgroundColor: Colors.white,
90+
),
91+
child: const Text('Open Text File'),
92+
onPressed: () => _openTextFile(context),
93+
),
94+
ElevatedButton(
95+
style: ElevatedButton.styleFrom(
96+
foregroundColor: Colors.blue,
97+
backgroundColor: Colors.white,
98+
),
99+
child: const Text('Open Directory'),
100+
onPressed: () => _openDirectory(),
101+
),
102+
],
103+
),
104+
),
105+
);
106+
}
107+
}
108+
109+
/// Widget that displays a text file in a dialog.
110+
class TextDisplay extends StatelessWidget {
111+
/// Default Constructor.
112+
const TextDisplay({super.key, required this.filename, required this.fileContents});
113+
114+
/// The name of the file.
115+
final String filename;
116+
117+
/// The contents of the file.
118+
final String fileContents;
119+
120+
@override
121+
Widget build(BuildContext context) {
122+
return AlertDialog(
123+
title: Text(filename),
124+
content: Scrollbar(child: SingleChildScrollView(child: Text(fileContents))),
125+
actions: <Widget>[
126+
TextButton(child: const Text('Close'), onPressed: () => Navigator.pop(context)),
127+
],
128+
);
129+
}
130+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Project-level configuration.
2+
cmake_minimum_required(VERSION 3.13)
3+
project(runner LANGUAGES CXX)
4+
5+
# The name of the executable created for the application. Change this to change
6+
# the on-disk name of your application.
7+
set(BINARY_NAME "cross_file_io_example")
8+
# The unique GTK application identifier for this application. See:
9+
# https://wiki.gnome.org/HowDoI/ChooseApplicationID
10+
set(APPLICATION_ID "dev.flutter.packages.cross_file_io_example")
11+
12+
# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
13+
# versions of CMake.
14+
cmake_policy(SET CMP0063 NEW)
15+
16+
# Load bundled libraries from the lib/ directory relative to the binary.
17+
set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
18+
19+
# Root filesystem for cross-building.
20+
if(FLUTTER_TARGET_PLATFORM_SYSROOT)
21+
set(CMAKE_SYSROOT ${FLUTTER_TARGET_PLATFORM_SYSROOT})
22+
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
23+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
24+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
25+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
26+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
27+
endif()
28+
29+
# Define build configuration options.
30+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
31+
set(CMAKE_BUILD_TYPE "Debug" CACHE
32+
STRING "Flutter build mode" FORCE)
33+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
34+
"Debug" "Profile" "Release")
35+
endif()
36+
37+
# Compilation settings that should be applied to most targets.
38+
#
39+
# Be cautious about adding new options here, as plugins use this function by
40+
# default. In most cases, you should add new options to specific targets instead
41+
# of modifying this function.
42+
function(APPLY_STANDARD_SETTINGS TARGET)
43+
target_compile_features(${TARGET} PUBLIC cxx_std_14)
44+
target_compile_options(${TARGET} PRIVATE -Wall -Werror)
45+
target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
46+
target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
47+
endfunction()
48+
49+
# Flutter library and tool build rules.
50+
set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
51+
add_subdirectory(${FLUTTER_MANAGED_DIR})
52+
53+
# System-level dependencies.
54+
find_package(PkgConfig REQUIRED)
55+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
56+
57+
# Application build; see runner/CMakeLists.txt.
58+
add_subdirectory("runner")
59+
60+
# Run the Flutter tool portions of the build. This must not be removed.
61+
add_dependencies(${BINARY_NAME} flutter_assemble)
62+
63+
# Only the install-generated bundle's copy of the executable will launch
64+
# correctly, since the resources must in the right relative locations. To avoid
65+
# people trying to run the unbundled copy, put it in a subdirectory instead of
66+
# the default top-level location.
67+
set_target_properties(${BINARY_NAME}
68+
PROPERTIES
69+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/intermediates_do_not_run"
70+
)
71+
72+
73+
# Generated plugin build rules, which manage building the plugins and adding
74+
# them to the application.
75+
include(flutter/generated_plugins.cmake)
76+
77+
78+
# === Installation ===
79+
# By default, "installing" just makes a relocatable bundle in the build
80+
# directory.
81+
set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
82+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
83+
set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
84+
endif()
85+
86+
# Start with a clean build bundle directory every time.
87+
install(CODE "
88+
file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
89+
" COMPONENT Runtime)
90+
91+
set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
92+
set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
93+
94+
install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
95+
COMPONENT Runtime)
96+
97+
install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
98+
COMPONENT Runtime)
99+
100+
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
101+
COMPONENT Runtime)
102+
103+
foreach(bundled_library ${PLUGIN_BUNDLED_LIBRARIES})
104+
install(FILES "${bundled_library}"
105+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
106+
COMPONENT Runtime)
107+
endforeach(bundled_library)
108+
109+
# Copy the native assets provided by the build.dart from all packages.
110+
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/linux/")
111+
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
112+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
113+
COMPONENT Runtime)
114+
115+
# Fully re-copy the assets directory on each build to avoid having stale files
116+
# from a previous install.
117+
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
118+
install(CODE "
119+
file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
120+
" COMPONENT Runtime)
121+
install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
122+
DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
123+
124+
# Install the AOT library on non-Debug builds only.
125+
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
126+
install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
127+
COMPONENT Runtime)
128+
endif()

0 commit comments

Comments
 (0)