Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Y
yaml_content
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
yaml_content
Commits
7cd43f15
Commit
7cd43f15
authored
7 years ago
by
Stephen Lucero
Browse files
Options
Downloads
Patches
Plain Diff
Incorporate drush commands from v1.
TO DO: - Test and confirm compatibility
parent
5180d752
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
yaml_content.drush.inc
+110
-49
110 additions, 49 deletions
yaml_content.drush.inc
with
110 additions
and
49 deletions
yaml_content.drush.inc
+
110
−
49
View file @
7cd43f15
<?php
/**
* @file
* Drush commands for the yaml_content module.
...
...
@@ -6,7 +7,7 @@
* @todo Re-implement as universal command compatible with Drupal console.
*/
use
\
Drupal\yaml_content
\ContentLoader\ContentLoader
;
use
Drupal\yaml_content
\ContentLoader\ContentLoader
Interface
;
/**
* Implements hook_drush_command().
...
...
@@ -17,21 +18,37 @@ function yaml_content_drush_command() {
$items
[
'yaml-content-import'
]
=
[
'description'
=>
dt
(
'Import yaml content.'
),
'aliases'
=>
[
'yci'
],
'arguments'
=>
[],
'options'
=>
[],
'arguments'
=>
[
'directory'
=>
dt
(
'The directory path where content files may be found.'
),
'file'
=>
dt
(
'(Optional) The name of a content file to be imported.'
),
],
'options'
=>
[
'create-new'
=>
dt
(
'Set this to create content even if it is already in the system.'
),
],
];
$items
[
'yaml-content-import-module'
]
=
[
'description'
=>
dt
(
'Import yaml content from a module.'
),
'aliases'
=>
[
'ycim'
],
'arguments'
=>
[],
'options'
=>
[],
'arguments'
=>
[
'module'
=>
dt
(
'The machine name of a module to be searched for content.'
),
'file'
=>
dt
(
'(Optional) The name of a content file to be imported.'
),
],
'options'
=>
[
'create-new'
=>
dt
(
'Set this to create content even if it is already in the system.'
),
],
];
$items
[
'yaml-content-import-dev'
]
=
[
'description'
=>
dt
(
'Dev debugging import.'
),
'aliases'
=>
[
'ycid'
],
'arguments'
=>
[],
'options'
=>
[],
$items
[
'yaml-content-import-profile'
]
=
[
'description'
=>
dt
(
'Import yaml content from a profile.'
),
'aliases'
=>
[
'ycip'
],
'arguments'
=>
[
'profile'
=>
dt
(
'The machine name of a profile to be searched for content.'
),
'file'
=>
dt
(
'(Optional) The name of a content file to be imported.'
),
],
'options'
=>
[
'create-new'
=>
dt
(
'Set this to create content even if it is already in the system.'
),
],
];
return
$items
;
...
...
@@ -39,30 +56,23 @@ function yaml_content_drush_command() {
/**
* Import specified yaml content file(s).
*
*
* @param string $directory
* The directory path containing the yaml content file(s) to be imported.
* @param string
|string[]
$file
* @param string $file
* (Optional) The name of a file to be imported or an array of files to
* import. If this argument is not provided then all files in the directory
* matching `*.content.yml` are queued for import.
*
* @todo Implement file globbing for optional `$file` parameter.
*/
function
drush_yaml_content_import
(
$directory
,
$file
=
NULL
)
{
$loader
=
\Drupal
::
service
(
'yaml_content.content_loader'
);
$loader
->
setContentPath
(
$directory
);
// @todo Implement file globbing if `$file` is NULL.
$import_files
=
is_array
(
$file
)
?
$file
:
[
$file
];
// Identify files for import.
$mask
=
'/'
.
(
isset
(
$file
)
?
$file
:
'.*'
)
.
'\.content\.yml/'
;
$files
=
drush_yaml_content_discover_files
(
$directory
.
'/content'
,
$mask
);
// @todo Verify files before loading for import.
foreach
(
$import_files
as
$file
)
{
drush_print_r
(
'Importing content: '
.
$file
);
$loader
->
parseContent
(
$file
);
$loaded
=
$loader
->
loadContent
();
drush_print_r
(
'Imported '
.
count
(
$loaded
)
.
' items.'
);
}
_drush_yaml_content_import_files
(
$loader
,
$files
);
}
/**
...
...
@@ -80,43 +90,94 @@ function drush_yaml_content_import($directory, $file = NULL) {
* matching `*.content.yml` are queued for import.
*/
function
drush_yaml_content_import_module
(
$module
,
$file
=
NULL
)
{
$path
=
drupal_get_path
(
'module'
,
$module
);
$loader
=
\Drupal
::
service
(
'yaml_content.content_loader'
);
$loader
->
setContentPath
(
$path
);
$path
=
drupal_get_path
(
'module'
,
$module
);
// Identify files for import.
$mask
=
'/'
.
(
isset
(
$file
)
?
$file
:
'.*'
)
.
'\.content\.yml/'
;
$files
=
drush_yaml_content_discover_files
(
$path
.
'/content'
,
$mask
);
if
(
!
$path
)
{
// @todo Handle when the module cannot be found.
}
else
{
$path
.
=
'/content'
;
}
_drush_yaml_content_import_files
(
$loader
,
$files
);
}
/**
* Import specified yaml content file(s) from a designated profile.
*
* @param string $profile
* The profile to look for content files within.
*
* This command assumes files will be contained within a `content/` directory
* at the top of the module's main directory. Any files within matching the
* pattern `*.content.yml` will then be imported.
* @param string|string[] $file
* (Optional) The name of a file to be imported or an array of files to
* import. If this argument is not provided then all files in the directory
* matching `*.content.yml` are queued for import.
*/
function
drush_yaml_content_import_profile
(
$profile
,
$file
=
NULL
)
{
$path
=
drupal_get_path
(
'profile'
,
$profile
);
$loader
=
\Drupal
::
service
(
'yaml_content.content_loader'
);
$loader
->
setContentPath
(
$path
);
// Identify files for import.
if
(
is_null
(
$file
))
{
$mask
=
'/.*\.content\.yml/'
;
}
else
{
// Scan only for the specific file name if it exists.
$mask
=
'/'
.
$file
.
'\.content\.yml/'
;
}
$files
=
file_scan_directory
(
$path
,
$mask
,
[
'recurse'
=>
FALSE
]);
$mask
=
'/'
.
(
isset
(
$file
)
?
$file
:
'.*'
)
.
'\.content\.yml/'
;
$files
=
drush_yaml_content_discover_files
(
$path
.
'/content'
,
$mask
);
_drush_yaml_content_import_files
(
$loader
,
$files
);
}
// @todo Verify files before loading for import.
foreach
(
$files
as
$filename
=>
$file
)
{
drush_print_r
(
'Importing content: '
.
$filename
);
$loader
->
parseContent
(
$file
->
filename
);
$loaded
=
$loader
->
loadContent
();
drush_print_r
(
'Imported '
.
count
(
$loaded
)
.
' items.'
);
}
/**
* Scan and discover content files for import.
*
* The scanner assumes all content files will follow the naming convention of
* '*.content.yml'.
*
* @param string $path
* The directory path to be scanned for content files.
* @param string $mask
* (Optional) A file name mask to limit matches in scanned files.
*
* @return array
* An associative array of objects keyed by filename with the following
* properties as returned by file_scan_directory():
*
* - 'uri'
* - 'filename'
* - 'name'
*
* @see file_scan_directory()
*/
function
drush_yaml_content_discover_files
(
$path
,
$mask
=
'/.*\.content\.yml/'
)
{
// Identify files for import.
$files
=
file_scan_directory
(
$path
,
$mask
,
[
'key'
=>
'filename'
,
'recurse'
=>
FALSE
,
]);
return
$files
;
}
/**
* Import debugging content.
* Import content files using a Content Loader.
*
* @param \Drupal\yaml_content\ContentLoader\ContentLoaderInterface $loader
* The content loader to use to import the referenced files.
* @param array $files
* An array of file descriptors as loaded by file_scan_directory() keyed by
* filename. Each of the listed files will be imported.
*/
function
drush_yaml_content_import_dev
()
{
$content_path
=
drupal_get_path
(
'module'
,
'yaml_content'
)
.
'/content'
;
drush_yaml_content_import
(
$content_path
,
'dev.content.yml'
);
function
_drush_yaml_content_import_files
(
ContentLoaderInterface
$loader
,
array
$files
)
{
// @todo Verify files before loading for import.
foreach
(
$files
as
$filename
=>
$file
)
{
drush_print_r
(
dt
(
'Importing content: @file'
,
[
'@file'
=>
$filename
,
]));
$loaded
=
$loader
->
loadContent
(
$filename
);
drush_print_r
(
dt
(
'Imported @count items'
,
[
'@count'
=>
count
(
$loaded
),
]));
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment